home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / draw / progs / drawgrab / !DrawGrab / CSource < prev    next >
Encoding:
Text File  |  1992-10-29  |  9.4 KB  |  508 lines

  1. #include "saveas.h"
  2. #include "sprite.h"
  3. #include "event.h"
  4. #include "werr.h"
  5. #include "wimpt.h"
  6. #include "menu.h"
  7. #include "win.h"
  8. #include "baricon.h"
  9. #include "flex.h"
  10. #include "dbox.h"
  11. #include "template.h"
  12. #include "trace.h"
  13. #include "res.h"
  14. #include "resspr.h"
  15. #include "bbc.h"
  16. #include "magnify.h"
  17. #include "wimp.h"
  18. #include "drawfdiag.h"
  19. #include "drawfobj.h"
  20. #include "drawmod.h"
  21. #include "xferrecv.h"
  22. #include "xfersend.h"
  23. #include "visdelay.h"
  24. #include <stdlib.h>
  25. #include <string.h>
  26.  
  27.  
  28. typedef enum
  29.  {
  30.  Mmain_info=1,Mmain_zoom,Mmain_save,Mmain_quit
  31.  } Mmain;
  32.  
  33. struct
  34.  {
  35.  wimp_w Wmain,Wsave;
  36.  menu Mmain,Mmain_s;
  37.  BOOL loaded;
  38.  draw_diag draw;
  39.  char title[256];
  40.  char save[256];
  41.  dbox Winfo;
  42.  int magmul,magdiv;
  43.  } X;
  44.  
  45. extern BOOL quit = FALSE;
  46.  
  47.  
  48.  
  49. void draw_to_sprite(sprite_area *sarea,int *id)
  50.  {
  51.  int r0,r1,r2,r3;
  52.  wimp_redrawstr r;
  53.  wimp_winfo w;
  54.  draw_diag *d = &X.draw;
  55.  draw_error error;
  56.  double mag = (double) X.magmul / (double) X.magdiv;
  57.  w.w = X.Wmain;
  58.  wimp_get_wind_info(&w);
  59.  r.w = w.w;
  60.  w.info.box.x1 -= w.info.box.x0;
  61.  w.info.box.y1 -= w.info.box.y0;
  62.  w.info.box.x0 = 0;
  63.  w.info.box.y0 = 0;
  64.  r.box = r.g = w.info.box;
  65.  r.scx = w.info.scx;
  66.  r.scy = w.info.scy;
  67.  bbc_move(0,0);
  68.  os_swi4r(0x2e,0x13c,(int) sarea,(int) id,0,&r0,&r1,&r2,&r3);
  69.  draw_render_diag(d,(draw_redrawstr *)&r,mag,&error);
  70.  os_swi4(0x2e,r0,r1,r2,r3);
  71.  }
  72.  
  73.  
  74.  
  75. BOOL save_sprite(char *file,void *h)
  76.  {
  77.  sprite_area *sarea;
  78.  sprite_id id;
  79.  sprite_ptr *ptr;
  80.  flex_ptr anchor;
  81.  int dx = wimpt_dx(),dy = wimpt_dy();
  82.  int xs,ys;
  83.  wimp_wstate wos;
  84.  char leaf[13] = "drawsprite";
  85.  strcpy(X.save,file);
  86.  wimpt_complain(wimp_get_wind_state(X.Wmain,&wos));
  87.  if (h);
  88.  saveas_read_leafname_during_send(leaf,13);
  89.  id.s.name = leaf;
  90.  id.tag = sprite_id_name;
  91.  xs = (wos.o.box.x1 - wos.o.box.x0) / dx + 1;
  92.  ys = (wos.o.box.y1 - wos.o.box.y0) / dy + 1;
  93.  if (!flex_alloc(anchor,xs*ys*2+1024))
  94.    {
  95.    werr(0,"Cannot allocate enough memory for sprite");
  96.    return FALSE;
  97.    }
  98.  sarea = *anchor;
  99.  sarea->sproff = 16;
  100.  sprite_area_initialise(sarea,xs*ys*2+512);
  101.  sprite_create_rp(sarea,leaf,TRUE,xs-1,ys-1,wimpt_mode(),ptr);
  102.  draw_to_sprite(sarea,(int *)*ptr);
  103.  sprite_area_save(sarea,X.save);
  104.  flex_free(anchor);
  105.  return TRUE;
  106.  }
  107.  
  108.  
  109.  
  110. void redraw_win(wimp_w handle)
  111.  {
  112.  int more;
  113.  double mag = (double) X.magmul / (double) X.magdiv;
  114.  wimp_redrawstr r;
  115.  BOOL errors = FALSE;
  116.  BOOL loaded = X.loaded;
  117.  draw_diag *d = &X.draw;
  118.  r.w = handle;
  119.  wimpt_noerr(wimp_redraw_wind(&r,&more));
  120.  while (more)
  121.    {
  122.    draw_error error;
  123.    if (loaded && d->data != NULL)
  124.      {
  125.      if (!draw_render_diag(d,(draw_redrawstr *)&r,mag,&error))
  126.        errors = TRUE;
  127.      }
  128.    wimp_get_rectangle(&r,&more);
  129.    }
  130.  }
  131.  
  132.  
  133.  
  134. void force_redraw(void)
  135.  {
  136.  wimp_redrawstr redraw;
  137.  if (!X.loaded || !X.draw.data) return;
  138.  draw_queryBox(&X.draw,(draw_box *)&redraw.box,TRUE);
  139. #define xw 16
  140.  redraw.box.x0 = redraw.box.x0 * X.magmul / X.magdiv - xw;
  141.  redraw.box.y0 = redraw.box.y0 * X.magmul / X.magdiv - xw;
  142.  redraw.box.x1 = redraw.box.x1 * X.magmul / X.magdiv + xw;
  143.  redraw.box.y1 = redraw.box.y1 * X.magmul / X.magdiv + xw;
  144.  redraw.w = X.Wmain;
  145.  wimpt_noerr(wimp_force_redraw(&redraw));
  146.  }
  147.  
  148.  
  149.  
  150. void event_hand(wimp_eventstr *e,void *handle)
  151.  {
  152.  handle = handle;
  153.  switch (e->e)
  154.    {
  155.    case wimp_ENULL:
  156.      break;
  157.    case wimp_EREDRAW:
  158.      wimpt_checkmode();
  159.      redraw_win(e->data.o.w);
  160.      break;
  161.    case wimp_EOPEN:
  162.      wimp_open_wind(&e->data.o);
  163.      break;
  164.    case wimp_ECLOSE:
  165.      wimp_close_wind(e->data.o.w);
  166.      break;
  167.    case wimp_EPTRLEAVE:
  168.      break;
  169.    case wimp_EPTRENTER:
  170.      break;
  171.    case wimp_EBUT:
  172.      break;
  173.    case wimp_EUSERDRAG:
  174.      break;
  175.    case wimp_EKEY:
  176.      break;
  177.    case wimp_EMENU:
  178.      break;
  179.    case wimp_ESCROLL:
  180.      break;
  181.    case wimp_ELOSECARET:
  182.      break;
  183.    case wimp_EGAINCARET:
  184.      break;
  185.    case wimp_ESEND:
  186.    case wimp_ESENDWANTACK:
  187.      break;
  188.    case wimp_EACK:
  189.      break;
  190.    }
  191.  }
  192.  
  193.  
  194.  
  195. void unload(void)
  196.  {
  197.  X.loaded = FALSE;
  198.  menu_setflags(X.Mmain,2,0,1);
  199.  menu_setflags(X.Mmain,3,0,1);
  200.  }
  201.  
  202.  
  203.  
  204. void dispose(void)
  205.  {
  206.  draw_diag *d = &X.draw;
  207.  if (X.loaded && d->data != NULL)
  208.    {
  209.    force_redraw();
  210.    flex_free((flex_ptr) &d->data);
  211.    d->length = 0;
  212.    }
  213.  unload();
  214.  }
  215.  
  216.  
  217.  
  218. void icon_click(wimp_i icon)
  219.  {
  220.  wimp_wstate state;
  221.  icon = icon;
  222.  if (!X.loaded) return;
  223.  wimpt_complain(wimp_get_wind_state(X.Wmain,&state));
  224.  state.o.behind = -1;
  225.  wimpt_noerr(wimp_open_wind(&state.o));
  226.  }
  227.  
  228.  
  229.  
  230. void load_error(void)
  231.  {
  232.  dispose();
  233.  X.title[0] = 0;
  234.  }
  235.  
  236.  
  237.  
  238. BOOL load(void)
  239.  {
  240.  os_filestr file;
  241.  file.action = 255;
  242.  file.name = X.title;
  243.  file.loadaddr = (int)X.draw.data;
  244.  file.execaddr = 0;
  245.  return (wimpt_complain(os_file(&file)) == 0);
  246.  }
  247.   
  248.  
  249.  
  250. int file_size(char *f)
  251.  {
  252.  os_filestr file;
  253.  file.action = 5;
  254.  file.name = f;
  255.  if (wimpt_complain(os_file(&file)) != 0) return 0;
  256.  switch (file.action)
  257.    {
  258.    case 0: werr(0,"File not found"); return 0;
  259.    case 2: werr(0,"I cannot load a directory"); return 0;
  260.    }
  261.  if ((file.loadaddr & 0xfff00000) == 0xfff00000)
  262.    return file.start;
  263.  else
  264.    {
  265.    werr(0,"File cannot be loaded");
  266.    return 0;
  267.    }
  268.  }
  269.  
  270.  
  271.  
  272. void load_finish(int size)
  273.  {
  274.  draw_error error;
  275.  if (draw_verify_diag(&X.draw,&error))
  276.    {
  277.    X.draw.length = size;
  278.    X.loaded = TRUE;
  279.    force_redraw();
  280.    }
  281.  else
  282.    {
  283.    werr(0,"Error in verifying Draw file");
  284.    load_error();
  285.    }
  286.  }
  287.  
  288.  
  289.  
  290. BOOL load_check(int ftype,int size)
  291.  {
  292.  if (ftype != 0xaff)
  293.    {
  294.    werr(0,"Not a Draw file");
  295.    return FALSE;
  296.    }
  297.  dispose();
  298.  if (flex_alloc((flex_ptr) &X.draw.data, size))
  299.    return TRUE;
  300.  else
  301.    {
  302.    werr(0,"Unable to allocate memory for new diagram");
  303.    return FALSE;
  304.    }
  305.  }
  306.  
  307.  
  308.  
  309. void load_file(void)
  310.  {
  311.  char *fname;
  312.  int size;
  313.  int ftype = xferrecv_checkinsert(&fname);
  314.  if (strlen(fname) > 256)
  315.    werr(0,"Cannot load file - filename is too long (max = 256 chars)");
  316.  else
  317.    {
  318.    strcpy(X.title,fname);
  319.    size = file_size(fname);
  320.    if (size > 0 && load_check(ftype,size))
  321.      {
  322.      visdelay_begin();
  323.      if (load())
  324.        {
  325.        load_finish(size);
  326.        }
  327.      else
  328.        load_error();
  329.      visdelay_end();
  330.      }
  331.    }
  332.  xferrecv_insertfileok();
  333.  menu_setflags(X.Mmain,2,0,0);
  334.  menu_setflags(X.Mmain,3,0,0);
  335.  }
  336.  
  337.  
  338.  
  339. void set_win(void)
  340.  {
  341.  wimp_redrawstr str;
  342.  draw_queryBox(&X.draw,(draw_box *)&str.box,TRUE);
  343.  str.box.x0 = str.box.x0 * X.magmul / X.magdiv - 16;
  344.  str.box.y0 = str.box.y0 * X.magmul / X.magdiv - 16;
  345.  str.box.x1 = str.box.x1 * X.magmul / X.magdiv + 16;
  346.  str.box.y1 = str.box.y1 * X.magmul / X.magdiv + 16;
  347.  str.w = X.Wmain;
  348.  str.scx = 0;
  349.  str.scy = 0;
  350.  if (str.g.x1 > str.box.x1) str.g.x1 = str.box.x1;
  351.  if (str.g.y1 > str.box.y1) str.g.y1 = str.box.y1;
  352.  wimp_set_extent(&str);
  353.  win_settitle(X.Wmain,X.title);
  354.  }
  355.  
  356.  
  357.  
  358. BOOL icon_select(wimp_w win,wimp_i icon)
  359.  {
  360.  int r;
  361.  wimp_icon i;
  362.  wimp_get_icon_info(win,icon,&i);
  363.  r = i.flags & wimp_ISELECTED;
  364.  if (r == 0) return FALSE;
  365.  else return TRUE;
  366.  }
  367.  
  368.  
  369.  
  370. void redraw_mag(void *h)
  371.  {
  372.  wimp_wstate wos;
  373.  h = h;
  374.  wimp_close_wind(X.Wmain);
  375.  set_win();
  376.  wimp_get_wind_state(X.Wmain,&wos);
  377.  wimp_open_wind(&wos.o);
  378.  force_redraw();
  379.  }
  380.  
  381.  
  382.  
  383. int print_err(char *f, void *h)
  384.  {
  385.  f = f;
  386.  h = h;
  387.  werr(0,"Use the !Paint application to print sprite files");
  388.  return xfersend_printFailed;
  389.  }
  390.  
  391.  
  392.  
  393. void icon_load(wimp_eventstr *e,void *handle)
  394.  {
  395.  wimp_wstate state;
  396.  handle = handle;
  397.  e = e;
  398.  load_file();
  399.  set_win();
  400.  wimpt_complain(wimp_get_wind_state(X.Wmain,&state));
  401.  state.o.behind = -1;
  402.  wimpt_noerr(wimp_open_wind(&state.o));
  403.  }
  404.  
  405.  
  406.  
  407. void icon_menu(void *handle,char *hit)
  408.  {
  409.  handle = handle;
  410.  switch ((Mmain) hit[0])
  411.    {
  412.    case Mmain_info:
  413.      dbox_show(X.Winfo);
  414.      dbox_fillin(X.Winfo);
  415.      dbox_hide(X.Winfo);
  416.      break;
  417.    case Mmain_zoom:
  418.      magnify_select(&X.magmul,&X.magdiv,9,9,redraw_mag,0);
  419.      break;
  420.    case Mmain_save:
  421.      saveas(0xff9,X.save,X.draw.length,save_sprite,0,print_err,0);
  422.      break;
  423.    case Mmain_quit:
  424.      quit = TRUE;
  425.      break;
  426.    }
  427.  }
  428.  
  429.  
  430.  
  431. BOOL create_win(char *name,wimp_w *h)
  432.  {
  433.  wimp_wind *window;
  434.  window = template_syshandle(name);
  435.  window->minsize = 0x00010001;
  436.  if (window == 0) return FALSE;
  437.  return (wimpt_complain(wimp_create_wind(window,h)) == 0);
  438.  }
  439.  
  440.  
  441.  
  442. BOOL init_wimp(void)
  443.  {
  444.  wimpt_init("Draw Grabber");
  445.  flex_init();
  446.  res_init("DrawGrab");
  447.  resspr_init();
  448.  template_init();
  449.  dbox_init();
  450.  
  451.  baricon("!DrawGrab",(int)resspr_area(),icon_click);
  452.  X.magmul = 1;
  453.  X.magdiv = 1;
  454.  strcpy(X.save,"DrawSprite");
  455.  
  456.  /* ************** */
  457.  /* create windows */
  458.  /* ************** */
  459.  
  460.  if (!create_win("main",&X.Wmain)) return FALSE;
  461.  X.Winfo = dbox_new("progInfo");
  462.  dbox_setfield(X.Winfo,1,"!DrawGrab");
  463.  dbox_setfield(X.Winfo,2,"Saves Draw files as Sprites");
  464.  dbox_setfield(X.Winfo,3,"© Paul Mason, 1992");
  465.  dbox_setfield(X.Winfo,4,"1.00 (27-Oct-1992)");
  466.  win_register_event_handler(X.Wmain,event_hand,&X.Wmain);
  467.  win_register_event_handler(win_ICONBARLOAD,icon_load,0);
  468.  
  469.  /* ************ */
  470.  /* create menus */
  471.  /* ************ */
  472.  
  473.  /* X.Mmain_s = menu_new("Options","Opt1,Opt2,Opt3"); */
  474.  X.Mmain = menu_new("DrawGrab",">Info,>Zoom,>Save sprite,Quit");
  475.  /* menu_submenu(X.Mmain,2,X.Mmain_s); */
  476.  menu_setflags(X.Mmain,Mmain_zoom,0,1);
  477.  menu_setflags(X.Mmain,Mmain_save,0,1);
  478.  if (!event_attachmenu(X.Wmain,X.Mmain,icon_menu,0)) return FALSE;
  479.  if (!event_attachmenu(win_ICONBAR,X.Mmain,icon_menu,0)) return FALSE;
  480.                      
  481.  /* ************** */
  482.  /* draw functions */
  483.  /* ************** */
  484.  
  485.  draw_registerMemoryFunctions(flex_alloc,flex_extend,flex_free);
  486.  X.draw.data = NULL;
  487.  X.draw.data = 0;
  488.  
  489.  return TRUE;
  490.  }          
  491.  
  492.  
  493.  
  494. void poll(void)
  495.  {
  496.  while (!quit)
  497.    event_process();
  498.  }
  499.  
  500.  
  501.  
  502. int main(void)
  503.  {
  504.  trace_on();
  505.  if (init_wimp() == FALSE) exit(1);
  506.  poll();
  507.  }
  508.