home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / demos / proart24 / PCA / !Processor / c / Processor
Encoding:
Text File  |  1996-08-28  |  10.4 KB  |  525 lines

  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdarg.h>
  6. #include "pca.h"
  7. #include "toolbox.h"
  8. #include "event.h"
  9. #include "wimplib.h"
  10. #include "gadgets.h"
  11. #include "window.h"
  12. #include "sprite.h"
  13. #include "ProgInfo.h"
  14. #include "wimp.h"
  15. #include "wimplib.h"
  16.  
  17. #define WimpVersion    310
  18.  
  19. static  WimpPollBlock  poll_block;
  20. static  MessagesFD     messages;
  21. static  IdBlock        id_block;
  22.  
  23. int OtherTask=0,ThisTask;
  24.  
  25. int handlers_attached=0;
  26.  
  27.     sprite_area * spblock;
  28.  
  29.     BOOL VALID=FALSE;
  30.     char Display[255];
  31.     char obname[100];
  32.     tag * anch;
  33.  
  34. typedef enum
  35.           {
  36.             red,
  37.             green
  38.           }operation;
  39.  
  40.  
  41. int change(int event_code, ToolboxEvent *event, IdBlock *id_block,void *handle);
  42. int quit_event(int event_code, ToolboxEvent *event, IdBlock *id_block,void *handle);
  43. int quit_message(WimpMessage *message,void *handle);
  44. int attach_handlers(int event_code,ToolboxEvent *event,IdBlock *id_block, void *handle);
  45. void werr(int fatal, char* format, ...);
  46. BOOL process(sprite_area *sp,sprite_ptr i,operation o,int value);
  47. void send_unhook(void);
  48. int close(int event_code, WimpPollBlock *event,IdBlock *id_block,void *v);
  49.  
  50.  
  51. /*
  52.  * Event handler to be called when toolbox event 1
  53.  * is generated (by click on the 'Quit' entry of
  54.  * the iconbar menu.
  55.  */
  56.  
  57. int quit_event(int event_code, ToolboxEvent *event, IdBlock *id_block,void *handle)
  58. {
  59.   event_code = event_code;
  60.   event = event;
  61.   id_block = id_block;
  62.   handle = handle;
  63.   send_unhook();
  64.   exit(0);
  65.   return(1);
  66. }
  67.  
  68. int close(int event_code, WimpPollBlock *event,IdBlock *id_block,void *v)
  69. {
  70. static char * fred="No Picture";
  71. ObjectId Oid;
  72.  
  73. Oid=(ObjectId)v;
  74.  
  75. displayfield_set_value (0,Oid,1,fred);
  76. send_unhook();
  77. anch=NULL;
  78. VALID=FALSE;
  79. wimp_close_window((int*)event);
  80. return FALSE;
  81. }
  82.  
  83.  
  84.  
  85.  
  86. int change(int event_code, ToolboxEvent *event, IdBlock *id_block,void *handle)
  87. {
  88.  
  89. char temp[8];
  90. int t;
  91.   event = event;
  92.   id_block = id_block;
  93.   handle = handle;
  94.  
  95.  
  96. switch (event_code)
  97.  
  98. {
  99.  case  Slider_ValueChanged:
  100.          switch(id_block->self_component)
  101.          {
  102.  
  103.  
  104.                case 2:
  105.  
  106.                       slider_get_value (0,id_block->self_id,id_block->self_component,&t);
  107.                       sprintf(temp,"%d",t );
  108.                       displayfield_set_value (0,id_block->self_id,6,temp);
  109.                       process((sprite_area *)(anch->base),(sprite_ptr)((int)anch->address+(int)anch->base) ,red,t);
  110.  
  111.  
  112.                break;
  113.  
  114.                 case 3:
  115.                       slider_get_value (0,id_block->self_id,id_block->self_component,&t);
  116.                       sprintf(temp,"%d",t );
  117.                       displayfield_set_value (0,id_block->self_id,7,temp);
  118.                       process((sprite_area *)(anch->base),(sprite_ptr)((int)anch->address+(int)anch->base) ,green,t);
  119.  
  120.                break;
  121.  
  122.         }
  123.  
  124.  
  125.          break;
  126.  
  127.  }
  128.  
  129.  
  130.  
  131.   return(1);
  132. }
  133.  
  134.  
  135.  
  136. /*
  137.  * Message handler to be called on receipt of a
  138.  * Quit or PreQuit message from the Wimp.
  139.  */
  140.  
  141.  
  142. int quit_message(WimpMessage *message,void *handle)
  143. {
  144.   message = message;
  145.   handle = handle;
  146.   send_unhook();
  147.   exit(0);
  148.   return(1);
  149. }
  150.  
  151. int Info(WimpMessage *message,void *handle)
  152. {
  153. message->hdr.your_ref=message->hdr.my_ref;
  154. sprintf(message->data.bytes,"Alter red/green levels. editing '%s'",obname);
  155. message->hdr.size=64;
  156. wimp_send_message(17,message,message->hdr.sender,0,NULL);
  157.  
  158. return(FALSE);
  159.  
  160. }
  161.  
  162. int WhosAbout_message(WimpMessage *message,void *handle)
  163.  
  164. {
  165. imhere_block *e;
  166. handle=handle;
  167.  
  168. e=(imhere_block *)message->data.words;
  169.  
  170. if(e->flags!=0xff9 /* Actually a filetype*/) return(0);
  171. e->flags=(Flag_spritename | Flag_info);
  172. e->id=1;
  173. strcpy(e->menu,"Tool");
  174. strcpy(e->sprite,"!processor"); /* add sprite name */
  175. message->hdr.action_code=Message_ImHere;
  176. message->hdr.size=84;
  177. wimp_send_message(17,message,message->hdr.sender,0,NULL);
  178. return(FALSE);
  179. }
  180.  
  181.  
  182. void send_changed()
  183. {
  184. donemystuff_block *e;
  185. WimpMessage w;
  186.  
  187. e=(donemystuff_block *)&w.data;
  188.  
  189. w.hdr.size=64;
  190. w.hdr.action_code=Message_DoneMyStuff;
  191. w.hdr.sender=ThisTask;
  192. e->tagptr=anch;
  193.  
  194. wimp_send_message      (17,&w,0,0,NULL);
  195. }
  196.  
  197. void send_unhook()
  198. {
  199. unhookme_block *e;
  200. WimpMessage w;
  201. if (OtherTask==0) return;
  202. e=(unhookme_block *)&w.data;
  203. w.hdr.size=64;
  204. w.hdr.action_code=Message_UnhookMe;
  205. w.hdr.sender=ThisTask;
  206. e->zero=0;
  207. e->reserved=0;
  208. e->handle=0;
  209. e->type=1;
  210. e->tagptr=anch;
  211. VALID=FALSE;
  212. wimp_send_message      (17,&w, OtherTask,0,NULL);
  213. OtherTask=0;
  214. }
  215.  
  216.  
  217.  
  218. int DoIt_message(WimpMessage *message,void *handle)
  219.  
  220. {
  221. char *p;
  222. message_editblk *e;
  223.  
  224. ObjectId Oid;
  225. Oid=(ObjectId)handle;
  226. e=(message_editblk *)message->data.words;
  227. anch=e->doyourstuff.tagptr;
  228.  
  229.  
  230. if (message->hdr.action_code==Message_DoYourStuff)
  231.    {
  232.       if (OtherTask!=message->hdr.sender)
  233.          {
  234.            send_unhook();
  235.            OtherTask=message->hdr.sender;
  236.          }
  237.       p=e->doyourstuff.name;
  238.       strcpy(obname,p);
  239.       sprintf(Display,"Editing picture %s",obname);
  240.       VALID=TRUE;
  241.    }
  242. else
  243.    {
  244.  
  245.        p=e->changed.newname;
  246.        if (*p!=NULL)
  247.           {
  248.              strcpy(obname,p);
  249.              sprintf(Display,"Editing picture %s",obname);
  250.           }
  251.    }
  252.  
  253. if (VALID)
  254. {
  255. toolbox_show_object(0,Oid,0,NULL,NULL,NULL);
  256.  
  257. if (p!=NULL) displayfield_set_value (0,Oid,1,Display);
  258.  
  259. }
  260. return(FALSE);
  261.  
  262. }
  263.  
  264.  
  265. int Invalid_message(WimpMessage *message,void *handle)
  266.  
  267. {
  268. ObjectId Oid;
  269.  
  270. message_editblk *e;
  271. _kernel_oserror * fault;
  272. static char * fred="No Picture";
  273.  
  274.  
  275. e=(message_editblk *)message->data.words;
  276.  
  277. Oid=(ObjectId)handle;
  278.  
  279.  
  280. handle=handle;
  281.  
  282. if(e->deselect.filetype!=0xff9) return(0);
  283.  
  284. anch=NULL;
  285. VALID=FALSE;
  286. OtherTask=0;
  287. fault=displayfield_set_value (0,Oid,1,fred);
  288. if (fault!=NULL) werr(FALSE,fault->errmess);
  289. return(TRUE);
  290.  
  291. }
  292.  
  293.  
  294.  
  295.  
  296. int main()
  297. {
  298.     int    toolbox_events = 0,
  299.            wimp_messages = 0,
  300.            event_code;
  301.  
  302.  
  303.  
  304.     /*
  305.      * register ourselves with the Toolbox.
  306.      */
  307.  
  308.     toolbox_initialise (0, WimpVersion, &wimp_messages, &toolbox_events, "<processor$Dir>",
  309.                         &messages, &id_block, 0, &ThisTask, 0);
  310.  
  311.  
  312.     /*
  313.      * initialise the event library.
  314.      */
  315.  
  316.     event_initialise (&id_block);
  317.  
  318.     event_set_mask (1+256);
  319.  
  320.  
  321.     /*
  322.      * register handler for toolbox event 1,
  323.      * which is generated by the 'Quit' option on the
  324.      * iconbar menu.  Also register message handlers
  325.      * to quit properly when quit messages are
  326.      * received from the wimp.
  327.      */
  328.  
  329.       event_register_toolbox_handler(-1,1,quit_event,0);
  330.       event_register_message_handler(Wimp_MQuit,quit_message,0);
  331.       event_register_message_handler(Wimp_MPreQuit,quit_message,0);
  332.  
  333.  
  334.       event_register_toolbox_handler(-1,Toolbox_ObjectAutoCreated,attach_handlers,NULL);
  335.  
  336.  
  337.  
  338.  
  339.  
  340.      spblock=NULL;
  341.  
  342.  
  343.     /*
  344.      * poll loop
  345.      */
  346.  
  347.     while (TRUE)
  348.     {
  349.         event_poll (&event_code, &poll_block, 0);
  350.     }
  351. }
  352.  
  353.  
  354.  
  355. int attach_handlers(int event_code,ToolboxEvent *event,IdBlock *id_block, void *handle)
  356. {
  357.   /* This function has been called as a result of an object being
  358.    * auto-created. For this example that means our iconbar object
  359.    * so we can now register all our other handlers for the rest
  360.    * of our objects
  361.    */
  362.  
  363.  
  364.   ObjectClass     obj_class;
  365.  
  366.  
  367.   toolbox_get_object_class(0,id_block->self_id,&obj_class);
  368.  
  369.  
  370.   switch(obj_class)
  371.   {
  372.     case Window_ObjectClass:
  373.  
  374.      if(  handlers_attached==0)
  375.     {
  376.      event_register_toolbox_handler(id_block->self_id,Slider_ValueChanged,change,NULL);
  377.      event_register_wimp_handler(-1,Wimp_ECloseWindow,close,(void*)id_block->self_id );
  378.      event_register_message_handler(Message_WhosAbout,WhosAbout_message,(void*)id_block->self_id );
  379.      event_register_message_handler(Message_DoYourStuff,DoIt_message,(void*)id_block->self_id );
  380.      event_register_message_handler(Message_Changed,DoIt_message,(void*)id_block->self_id);
  381.      event_register_message_handler(Message_Deselect,Invalid_message,(void*)id_block->self_id );
  382.      event_register_message_handler(Message_Info,Info,(void*)id_block->self_id);
  383.  
  384.      }
  385.             handlers_attached=1;
  386.  
  387.  
  388.      break;
  389.  
  390.    case ProgInfo_ObjectClass:
  391.  
  392.       proginfo_set_version (0,id_block->self_id, "0.02 " __DATE__);
  393.       break;
  394.  
  395.   }
  396.  
  397.  
  398.   return 1;
  399. }
  400.  
  401.  
  402.  
  403.  
  404.  
  405. void werr(int fatal, char* format, ...)
  406. {
  407.    va_list va;
  408.    _kernel_oserror e;
  409.  
  410.    e.errnum = 0;
  411.    va_start(va, format);
  412.    vsprintf(&e.errmess[0], format, va);
  413.    va_end(va);
  414.    wimp_report_error(&e, 0, "ReadMe",0,0,0);
  415.    if (fatal) exit(1);
  416. }
  417.  
  418.  
  419. BOOL process(sprite_area *sp,sprite_ptr i,operation o,int value)
  420. {
  421. static int current[2]={0,0};
  422.  
  423.  
  424. int *image;
  425. int width,height,v_dpi,h_dpi,type;
  426. sprite_header* s_h;
  427.  
  428. if (!VALID) return FALSE;
  429.  
  430. s_h=(sprite_header*)i;
  431.  
  432. s_h=(sprite_header*)i;
  433. width=s_h->width+1;
  434. height=s_h->height+1;
  435. type=(s_h->mode >> 27) & 0x1f;
  436. v_dpi=(s_h->mode >>14) & 0x1fff;
  437. h_dpi=(s_h->mode >>1)  & 0x1fff;
  438. image=(int*) ((int)s_h+(int)s_h->image);
  439. switch (type)
  440.   {
  441.  
  442.    case 6:    break;
  443.  
  444.    default:
  445.               werr(FALSE,"Unable to process sprites of type %d",type);
  446.               return(FALSE);
  447.               break;
  448.      }
  449.  
  450.  
  451.  
  452. switch(o)
  453. {
  454. int x,y,t;
  455. sprite_colour col;
  456. int r,g,b;
  457.  
  458.  case red:
  459.  
  460.                 t=value-current[0];
  461.                 if(t==0) return(FALSE);
  462.                for (x=0;x<width;x++)
  463.                    {
  464.                    for(y=0;y<height;y++)
  465.                       {
  466.                           col.colour=*image;
  467.                           r=col.colour & 0xff;
  468.                           g=(col.colour & 0xff00)>>8;
  469.                           b=(col.colour & 0xff0000)>>16;
  470.  
  471.                            r+=t/4;
  472.  
  473.                            r=(r & 255);
  474.                            g=(g % 256)<<8;
  475.                            b=(b % 256)<<16;
  476.                           col.colour=b | g | r;
  477.  
  478.  
  479.                           *image++=col.colour;
  480.                        }
  481.                     }
  482.                    current[0]=value;
  483.                break;
  484.  case green:
  485.                t=value-current[1];
  486.                if(t==0) return(FALSE);
  487.  
  488.                for (x=0;x<width;x++)
  489.                    {
  490.                    for(y=0;y<height;y++)
  491.                       {
  492.  
  493.                           col.colour=*image;
  494.                           r=col.colour & 0xff;
  495.                           g=(col.colour & 0xff00)>>8;
  496.                           b=(col.colour & 0xff0000)>>16;
  497.  
  498.  
  499.                            g+=t/4;
  500.  
  501.                            r=(r & 255);
  502.                            g=(g & 255)<<8;
  503.                            b=(b & 255)<<16;
  504.                           col.colour=b | g | r;
  505.  
  506.                           *image++=col.colour;
  507.  
  508.  
  509.                        }
  510.                     }
  511.  
  512.                      current[1]=value;
  513.                break;
  514.  
  515. }
  516.  
  517. send_changed();
  518.  
  519.  
  520. return(TRUE);
  521. }
  522.  
  523.  
  524.  
  525.