/*
 * Name   : RendDraw.c
 * Desc   : Main control for the RendDraw application
 * Author : James Bye
 * Date   : 6th April 1992
 *
 *
 * **********************************************
 * * Disclaimer                                 *
 * * ----------                                 *
 * *                                            *
 * * The software is provided "as is" Acorn     *
 * * Computers Limited ("Acorn") makes no       *
 * * warranty, express or implied, of the       *
 * * merchantability of this software or its    *
 * * fitness for any particular purpose. In no  *
 * * circumstances shall Acorn be liable for any*
 * * damage, loss of profits, or any indirect or*
 * * consequential loss arising out of the use  *
 * * of this software or inability to use this  *
 * * software, even if Acorn has been advised of*
 * * the possibility of such loss.              *
 * **********************************************
 */

#include "wimp.h"        /*  access to WIMP SWIs                      */
#include "wimpt.h"       /*  wimp task facilities                     */
#include "win.h"         /*  registering window handlers              */
#include "event.h"       /*  poll loops, etc                          */
#include "baricon.h"     /*  putting icon on icon bar                 */
#include "sprite.h"      /*  sprite operations                        */
#include "werr.h"        /*  error reporting                          */
#include "res.h"         /*  access to resources                      */
#include "resspr.h"      /*  sprite resources                         */
#include "flex.h"        /*  dynamic mem alloc from WIMP              */
#include "template.h"    /*  reading in template file                 */
#include "bbc.h"         /*  olde-style graphics routines             */
#include "colourtran.h"  /*  interface to colour translation module   */
#include "os.h"          /*  low-level RISCOS access                  */
#include "dbox.h"        /*  dialogue box handling                    */
#include "saveas.h"      /*  data export from dbox by icon dragging   */
#include "visdelay.h"    /*  show the hourglass for delay             */ 
                  
#define  TRACE 1
#include "trace.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>    

#include "drawfile.h"
#include "file.h"


/*-- menu definitions --*/

#define m_IconBar_Title "RendDraw"
#define m_IconBar_Hits  ">Info,Quit"
#define m_IconBar_Info  1
#define m_IconBar_Quit  2

/*-- static variables --*/

static menu   iconbar_menu;
static wimp_w display;
static BOOL   display_open = FALSE;

static DrawFile_Data drawfile = NULL;
static int   drawfile_size = 0;      

#define XCoord 0
#define YCoord 0


/*-- you should set the flatness to 200/current_scale_factor --*/

#define FLATNESS 200

/**************************************************
 * Static functions                               *
 **************************************************/

/*
 * loads the draw file
 */

static BOOL load_drawfile ( char *filename )
{                                    
_kernel_oserror *e;

  /*-- read the size of the draw file --*/

  e = file_readcat(filename,NULL,NULL,NULL,&drawfile_size,NULL);

  if(e != NULL)
  {
    werr(0,"%s",e->errmess);
    return(FALSE);
  }
 
  drawfile = malloc(drawfile_size);

  if(!drawfile)
  {
    werr(0,"Malloc failed");
    drawfile_size = 0;
    return(FALSE);
  }

  /*-- load the file and set up structures --*/

  file_load(filename,(int)drawfile);                

  return(TRUE);
}

 
  

 

/*
 * redraw handle for the display window
 */

static void display_redraw ( void )
{
wimp_redrawstr r;
BOOL more = FALSE;
DrawFile_ClipBBoxStr clip;
                  
  r.w = display;
  wimp_redraw_wind(&r,&more);

  while(more)
  {                       
    memcpy(&clip,&r.g,sizeof(DrawFile_ClipBBoxStr));
    DrawFile_Render(0 | DrawFile_RenderBoundingBoxes | DrawFile_SpecifyDefaultFlatness,
         drawfile,drawfile_size,
         DrawFile_UseIdentityTransMat,&clip,
         r.box.x0 - r.scx,r.box.y1 - r.scy,FLATNESS);
    wimp_get_rectangle(&r,&more);
  }
}


/*
 * event handler for the display window
 */

static void display_events ( wimp_eventstr *e, void *handle )
{
  handle = handle;

  switch(e->e)
  {
    case wimp_EOPEN  : wimp_open_wind(&e->data.o);
                       break;
    case wimp_ECLOSE : wimp_close_wind(display);
                       wimp_delete_wind(display);
                       display_open = FALSE;
                       if(drawfile)
                         free(drawfile);
                       break;
    case wimp_EREDRAW: display_redraw();
                       break;
  }
}


/*
 * shows the display window
 */                                

static void show_display ( char *filename )
{
  if(!display_open)
  {
    wimp_wind *wind = template_syshandle("display");
    wimp_wstate state;

     /*-- and create the window --*/
 
     wimp_create_wind(wind,&display);

     /*-- get window state and open the window --*/

     wimp_get_wind_state(display,&state);
     state.o.behind = -1;
     wimp_open_wind(&state.o);

     /*-- attach our event handler --*/

     win_register_event_handler(display,display_events,NULL);
     display_open = TRUE;
  }
  else
   werr(0,"Already displaying");
}



/*
 * shows the info box
 */

extern void show_info ( void )
{
dbox d = dbox_new("info");

  if(d == NULL)
    werr(1,"No memory for DBox");
  dbox_show(d);
  dbox_fillin(d);
  dbox_dispose(&d);
}



/*
 * click proc for the iconbar
 */

static void click_proc ( wimp_i i )
{
  i = i;
  werr(0,"Drag a draw file to the RendDraw icon");
}


/*
 * menu handler for the iconbar
 */

static void iconbar_menu_events ( void *handle, char *hit )
{                          
  handle = handle;
  switch(hit[0])
  {
     case m_IconBar_Info : show_info();
                           break;
     case m_IconBar_Quit : exit(0);
                           break;
     default             : break;
  }
}


/*
 * loader for the iconbar
 */

static void iconbar_loader ( wimp_eventstr *e, void *handle )
{
  handle = handle;
  switch(e->e)
  {
     case wimp_ESEND :
     case wimp_ESENDWANTACK :

       switch(e->data.msg.hdr.action)
       {
         case wimp_MDATALOAD :
         case wimp_MDATASAVE :
         case wimp_MDATAOPEN : /*-- load if it is a drawfile --*/
                               if(e->data.msg.data.dataload.type == 0xAFF)
                               {
                                 if(load_drawfile(e->data.msg.data.dataload.name))
                                   show_display(e->data.msg.data.dataload.name);
                               }
                               else
                                 werr(0,"RendDraw can only display drawfiles");
                               break;
         default             : break;
       } break;

     default : break;
  }
}
  

/*
 * unknown event handler for the application
 */

static BOOL our_unknowns ( wimp_eventstr *e, void *handle )
{
  handle = handle;
  switch(e->e)
  {
    case wimp_MMODECHANGE : /*-- recache mode vars on mode change --*/
                            wimpt_checkmode();                        
                            return(TRUE);
                            break;
  }
  return(FALSE);
}


/**************************************************
 * Main C function                                *
 **************************************************/

int main ( void )
{
  /*-- init ourselves with the wimp --*/

  wimpt_init("Render Drawfile");
  res_init("RendDraw");
  resspr_init();
  flex_init();
  template_init();
  dbox_init();
  visdelay_init();

  trace_on();

  /*-- place the icon on the iconbar --*/

  baricon("!RendDraw",1,click_proc);

  /*-- create & attach the menu to the iconbar --*/
                
  iconbar_menu = menu_new(m_IconBar_Title,m_IconBar_Hits);
  event_attachmenu(win_ICONBAR,iconbar_menu,iconbar_menu_events,NULL);

  /*-- attach unknown event handler for the application --*/ 

  win_add_unknown_event_processor(our_unknowns,NULL);

  /*-- attach an event handler to the iconbar --*/

  win_register_event_handler(win_ICONBARLOAD,iconbar_loader,NULL);
  
  /*-- inifinite polling loop --*/

  while(TRUE)
    event_process();
}

/*-- end --*/

