home *** CD-ROM | disk | FTP | other *** search
- #include <stdlib.h>
- #include <stdio.h>
- #include "wimp.h"
- #include "toolbox.h"
- #include "event.h"
- #include "wimplib.h"
- #include "gadgets.h"
- #include "window.h"
- #include "sprite.h"
- #include "ProgInfo.h"
- #include "menu.h"
- #include "swis.h"
-
- #include <string.h>
- #include <stdarg.h>
-
- #include "pca.h"
- #include "flex.h"
-
-
-
-
- #define WimpVersion 310
-
- static WimpPollBlock poll_block;
- static MessagesFD messages;
- static IdBlock id_block;
- static ObjectId toolmenu;
- static ObjectId mainmenu;
-
-
- int OtherTask,ThisTask;
-
- int handlers_attached=0;
-
- sprite_area * spblock;
- sprite_id id;
- char * sname ="tester";
-
- BOOL VALID=FALSE;
- BOOL SHORTCUT=FALSE;
- char Display[255];
- int count=0;
- char *names[64];
- int task[64][2];
- int *fake;
- BOOL ACCEPT_INPLACE=TRUE;
- BOOL HOOKED=FALSE;
- int hooked_w;
- int hooked_t;
- int hooked_r;
- void * invisible;
- tag *anch;
-
- int quit_event(int event_code, ToolboxEvent *event, IdBlock *id_block,void *handle);
- int quit_message(WimpMessage *message,void *handle);
- int attach_handlers(int event_code,ToolboxEvent *event,IdBlock *id_block, void *handle);
- void werr(int fatal, char* format, ...);
- int redraw_window(int event_code, WimpPollBlock *event,IdBlock *id_block,void *v);
- void invalidate_addresses(void);
- int WhosAbout_message(WimpMessage *message,void *handle);
- void blockmoved(BOOL b4,void *handle);
- int do_the_free(int event_code, ToolboxEvent *event, IdBlock *id_block,void *handle);
- int set_status(int event_code, ToolboxEvent *event, IdBlock *id_block,void *handle);
- int setup_menu(int event_code, ToolboxEvent *event, IdBlock *id_block,void *handle);
- int changed(WimpMessage *message,void *handle);
- int hereIam(int event_code, ToolboxEvent *event, IdBlock *id_block,void *handle);
- int update_window(int w_handle,BBox *box);
- void os_byte(int code,int * x,int *y);
- int akbd_pollsh(void);
- int akbd_pollctl(void);
- int button(int event_code, WimpPollBlock *event,IdBlock *id_block,void *v);
- void screen_to_sprite(WimpMouseClickEvent * c);
- int open(int event_code, WimpPollBlock *event,IdBlock *id_block,void *v);
- void store_gadget(char* Template,ObjectId Oid,ComponentId g);
- void create_gadget(ObjectId Oid,ComponentId g);
- void kill_gadget(ObjectId Oid,ComponentId g);
- void djdebug(char * msgs,...);
-
-
-
-
- /*
- * Event handler to be called when toolbox event 1
- * is generated (by click on the 'Quit' entry of
- * the iconbar menu.)
- */
-
- int quit_event(int event_code, ToolboxEvent *event, IdBlock *id_block,void *handle)
- {
- _kernel_swi_regs r;
- event_code = event_code;
- event = event;
- id_block = id_block;
- handle = handle;
- invalidate_addresses();
- r.r[0]=(int)anch;
- _kernel_swi(PCA_DeleteTag,&r,&r);
- exit(0);
- return(1);
- }
-
-
-
-
-
-
- /*
- * Message handler to be called on receipt of a
- * Quit or PreQuit message from the Wimp.
- */
-
- int quit_message(WimpMessage *message,void *handle)
- {
- _kernel_swi_regs r;
- message = message;
- handle = handle;
- invalidate_addresses();
- r.r[0]=(int)anch;
- _kernel_swi(PCA_DeleteTag,&r,&r);
-
-
-
-
- exit(0);
- return(1);
- }
-
-
-
- /* Called on receipt of a WhosAbout message. Add the new task to the
- utilities menu
- */
-
- int WhosAbout_message(WimpMessage *message,void *handle)
-
- {
- imhere_block *e;
- MenuTemplateEntry Entry = {0};
- int temp;
- char* q;
- handle=handle;
-
- e=(imhere_block *)message->data.words;
-
- temp=strlen(e->menu);
- q=(char*)malloc(temp+1);
- if (q==0) return(0);
- names[count]=q;
- strcpy(names[count],e->menu);
- task[count][0]=message->hdr.sender;
- task[count][1]=e->flags;
- Entry.click_event = 0x300;
- Entry.text = names[count];
- Entry.max_text = strlen (Entry.text) +1;
- Entry.component_id = (ComponentId) count++;
- menu_add_entry(0,(ObjectId)handle ,Menu_AddEntryAtEnd,(char *) &Entry,0);
- menu_set_fade(0,mainmenu,0,0);
-
- return(FALSE);
- }
-
-
-
- /* Called on receipt of an open window request. If acting as a local to
- an in place editor, keep it informed of windows position
- */
-
- int open(int event_code, WimpPollBlock *event,IdBlock *id_block,void *v)
- {
- objectpos_block *e;
-
- WimpMessage message;
-
- if(HOOKED)
- {
- e=(objectpos_block *)message.data.words;
- message.hdr.your_ref=hooked_r;
- message.hdr.size=64;
- message.hdr.action_code=Message_ObjectPosition;
- message.hdr.sender=ThisTask;
- e->tagptr=anch;
-
- e->handle=hooked_w;
- e->toolbarhandle=-1;
- e->zero=0;
- e->xlow=event->open_window_request.visible_area.xmin;
- e->ylow=event->open_window_request.visible_area.ymin;
- e->xscale=1<<16;
- e->yscale=1<<16;
- e->xlowu=event->open_window_request.visible_area.xmin;
- e->ylowu=event->open_window_request.visible_area.ymin;
- wimp_send_message(17,&message,task[hooked_t][0],0,NULL);
-
- }
- wimp_open_window((WimpOpenWindowBlock*)event);
-
- return(TRUE);
- }
-
-
- int close(int event_code, WimpPollBlock *event,IdBlock *id_block,void *v)
- {
- invalidate_addresses();
- HOOKED=FALSE;
- VALID=FALSE;
- wimp_close_window((int*)event);
- return FALSE;
- }
-
- void store_gadget(char* Template,ObjectId Oid,ComponentId g)
- {
- void *v,*t;
- int s;
- v=malloc(sizeof(ObjectTemplateHeader));
- t=malloc(sizeof(Button) +sizeof(GadgetHeader));
- if ((!t)||(!v)) werr(TRUE,"Fatal error: Insufficient memory");
- toolbox_template_lookup(0,Template,&v);
- window_extract_gadget_info(0,v,g,&t,&s);
- invisible=malloc(s);
- if (!invisible) werr(TRUE,"Fatal error: Insufficient memory");
- *(Gadget*)invisible=*(Gadget*)t;
- window_remove_gadget(0,Oid,g);
-
- }
-
- void create_gadget(ObjectId Oid,ComponentId g)
- {
- window_add_gadget(0,Oid,invisible,&g);
- }
-
- void kill_gadget(ObjectId Oid,ComponentId g)
- {
- window_remove_gadget(0,Oid,g);
- }
-
-
-
- /*
- A HookMe message has been received. Set the invisible icon's
- button type to that requested by the linked tool.
- */
-
- int hookme_message(WimpMessage *message,void *handle)
-
- {
- hookme_block *e;
- objectpos_block *f;
- int w;
- int clear,eor;
- ObjectId Oid;
- WimpGetWindowStateBlock state;
-
-
- Oid=(ObjectId)handle;
- create_gadget(Oid,0);
-
- e=(hookme_block *)message->data.words;
-
- eor=e->flags >> 16;
- clear=0xf000;
- button_set_flags (0,Oid,0,clear,eor);
-
-
- hooked_w=e->handle;
- message->hdr.your_ref=message->hdr.my_ref;
- hooked_r=message->hdr.your_ref;
- message->hdr.size=64;
- message->hdr.action_code=Message_ObjectPosition;
-
- window_get_wimp_handle(0,Oid ,&w);
- state.window_handle = w;
- wimp_get_window_state(&state);
-
- f=(objectpos_block*) e;
- f->handle=w;
- f->zero=0;
- f->toolbarhandle=-1;
- f->xlow=state.visible_area.xmin;
- f->ylow=state.visible_area.ymin;
- f->xscale=1<<16;
- f->yscale=1<<16;
- f->xlowu=state.visible_area.xmin;
- f->ylowu=state.visible_area.ymin;
-
- wimp_send_message (17,message,task[hooked_t][0],0,NULL);
- HOOKED=TRUE;
-
- return(FALSE);
- }
-
-
-
-
- /*
- Unhook and kill gadget
- */
-
- int unhookme_message(WimpMessage *message,void *handle)
- {
-
- ObjectId Oid;
-
- Oid=(ObjectId)handle;
-
-
- kill_gadget(Oid,0);
- HOOKED=FALSE;
-
- return(FALSE);
- }
-
-
-
-
-
-
- /* Registered callback function. Called when our flex block moves.
- Called twice, once before the move and once after. We are only interested
- in the call after.
- */
-
- void blockmoved(BOOL b4,void *handle)
- {
-
- if (!b4)
- {
-
- id.tag=sprite_id_name;
- id.s.addr=sname;
- sprite_select_rp(spblock,&id,&id.s.addr);
- id.tag=sprite_id_addr;
- anch->base=(int*)spblock;
- }
- }
-
-
-
- /* If the dummy block has not yet been freed, do it now. This will cause our
- main block to move, thus calling the registered callback handler.
- */
-
-
- int do_the_free(int event_code, ToolboxEvent *event, IdBlock *id_block,void *handle)
-
- {
- if(fake!=NULL)
- {
- flex_free((flex_ptr)&fake);
- fake=NULL;
- }
-
- /* There's only one dummy block to delete, so once it's been done,
- fade the menu entry.
- */
-
-
- menu_set_fade(0,mainmenu,1,1);
-
-
- return(TRUE);
- }
-
-
-
- /*
- Toggle user option to accept in place editing.
- */
-
- int set_status(int event_code, ToolboxEvent *event, IdBlock *id_block,void *handle)
-
- {
-
- ACCEPT_INPLACE=!ACCEPT_INPLACE;
-
- if(ACCEPT_INPLACE)
- menu_set_tick(0,mainmenu,2,1);
- else
- menu_set_tick(0,mainmenu,2,0);
-
- return(TRUE);
-
-
- }
-
-
-
- /* Main menu about to be shown. Send messages to find out who's about.
- Utility menu created by function handling responses.
- */
-
- int setup_menu(int event_code, ToolboxEvent *event, IdBlock *id_block,void *handle)
- {
- whosabout_block *e;
- WimpMessage w;
-
- int i;
- menu_set_fade(0,mainmenu,0,1);
- for (i=0;i<count;i++)
- {
- menu_remove_entry(0,(ObjectId)handle,i);
- free(names[i]);
- }
- count=0;
- e=(whosabout_block *)&w.data;
- w.hdr.size=64;
- w.hdr.action_code=Message_WhosAbout;
- w.hdr.my_ref=0;
- w.hdr.your_ref=0;
- w.hdr.sender=ThisTask;
-
- e->tagptr=anch;
- e->filetype=0xff9;
- wimp_send_message (17,&w,0,0,NULL);
-
- if(ACCEPT_INPLACE) menu_set_tick(0,mainmenu,2,1); else menu_set_tick(0,mainmenu,2,0);
-
- return(FALSE);
- }
-
-
-
-
- /* A remote application has modified the picture */
-
-
- int changed(WimpMessage *message,void *handle)
-
- {
- ObjectId Oid;
- BBox b;
- updatearea_block *e;
- int w;
-
- e=(updatearea_block *)message->data.words;
-
- Oid=(ObjectId)handle;
-
- if (message->hdr.action_code==Message_DoneMyStuff)
- {
- b.xmin=0;
- b.xmax=1000;
- b.ymin=-398;
- b.ymax=-00;
-
- }
- else
- {
- b.xmin=e->r.xlow;
- b.xmax=e->r.xhi;
- b.ymin=e->r.ylow-398;
- b.ymax=e->r.yhi-398;
- }
-
- window_get_wimp_handle(0,Oid ,&w);
- update_window(w,&b);
-
-
- return(TRUE);
- }
-
-
-
-
- int returned_message(WimpMessage *message,void *handle)
-
- {
- handle=handle;
-
- if ((message->hdr.sender==ThisTask) && SHORTCUT)
- {
-
- SHORTCUT=FALSE;
- toolbox_show_object(1,toolmenu,0,NULL,NULL,NULL);
-
-
- }
- return(FALSE);
- }
-
-
-
-
-
-
-
-
-
-
-
- /* link to selected remote */
-
- int hereIam(int event_code, ToolboxEvent *event, IdBlock *id_block,void *handle)
- {
- doyourstuff_block *e;
- WimpMessage w;
-
- e=(doyourstuff_block *)&w.data;
-
- w.hdr.size=64;
- w.hdr.action_code=Message_DoYourStuff;
- w.hdr.my_ref=0;
- w.hdr.your_ref=0;
- w.hdr.sender=ThisTask;
- e->toolid=1;
- e->flags=task[id_block->self_component][1];
- if (!ACCEPT_INPLACE) e->flags = e->flags & !Flag_remote;
- strcpy(e->name,"Gee-gee");
-
- e->tagptr=anch;
- e->filetype=0xff9;
- hooked_t=id_block->self_component;
- wimp_send_message (17,&w,task[id_block->self_component][0],0,NULL);
- VALID=TRUE;
- return(FALSE);
- }
-
-
-
- /* The picture is about to disappear, so warn remotes that the address they
- have will not longer be valid.
- */
-
-
-
-
- void invalidate_addresses(void)
- {
- deselect_block *e;
- WimpMessage w;
-
- e=(deselect_block *)&w.data;
-
- w.hdr.size=64;
- w.hdr.action_code=Message_Deselect;
- w.hdr.my_ref=0;
- w.hdr.your_ref=0;
- w.hdr.sender=ThisTask;
-
- e->tagptr=anch;
- e->filetype=0xff9;
- wimp_send_message (17,&w,0,0,NULL);
- VALID=FALSE;
- }
-
-
-
-
- int main()
- {
- int toolbox_events = 0,
- wimp_messages = 0,
- event_code;
- _kernel_swi_regs r;
-
-
- /*
- * register ourselves with the Toolbox.
- */
-
- toolbox_initialise (0, WimpVersion, &wimp_messages, &toolbox_events, "<Processed$Dir>",
- &messages, &id_block, 0, &ThisTask, 0);
-
-
- /* Initialise flex with dynamic areas active */
-
- flex_initx("Processed",(int*)&messages,TRUE,1024*1024,FALSE);
-
-
- /*
- * initialise the event library.
- */
-
- event_initialise (&id_block);
- event_set_mask (1+256);
-
-
- /*
- * register handler for toolbox event 1,
- * which is generated by the 'Quit' option on the
- * iconbar menu. Also register message handlers
- * to quit properly when quit messages are
- * received from the wimp.
- */
-
- event_register_toolbox_handler(-1,1,quit_event,0);
- event_register_message_handler(Wimp_MQuit,quit_message,0);
- event_register_message_handler(Wimp_MPreQuit,quit_message,0);
- event_register_toolbox_handler(-1,Toolbox_ObjectAutoCreated,attach_handlers,NULL);
-
-
-
-
-
- /* Allocate a block before the one containing the sprite. When this is later
- freed, it will cause the sprite to move.
- */
-
- flex_alloc((flex_ptr)&fake,100*1024);
-
-
- if (flex_alloc((flex_ptr)&spblock,300*1024)==0) werr(TRUE,"Fatal error. No room for sprite area");
-
-
- sprite_area_initialise(spblock, 1024*300);
- sprite_area_load(spblock, "<Processed$Dir>.Testfile");
- id.tag=sprite_id_name;
- id.s.addr=sname;
- sprite_select_rp(spblock,&id,&id.s.addr);
- id.tag=sprite_id_addr;
- flex_register((flex_ptr)&spblock,blockmoved,NULL);
-
- r.r[1]=(int)spblock;
- r.r[2]=(int)id.s.addr-(int)spblock;
- _kernel_swi(PCA_CreateTag,&r,&r);
- anch=(tag*)r.r[0];
-
-
- /*
- * poll loop
- */
-
- while (TRUE)
- {
- event_poll (&event_code, &poll_block, 0);
- }
- }
-
-
-
- int attach_handlers(int event_code,ToolboxEvent *event,IdBlock *id_block, void *handle)
- {
- /* This function has been called as a result of an object being
- * auto-created. For this example that means our iconbar object
- * so we can now register all our other handlers for the rest
- * of our objects
- */
-
-
- ObjectClass obj_class;
- char buffer[32];
- int dum;
-
-
-
- toolbox_get_object_class(0,id_block->self_id,&obj_class);
- toolbox_get_template_name(0,id_block->self_id,buffer,32,&dum);
-
-
- switch(obj_class)
- {
- case Window_ObjectClass:
-
- if ((!handlers_attached) && (strcmp(buffer,"Window")==0) )
- {
- event_register_wimp_handler(id_block->self_id,Wimp_ERedrawWindow,redraw_window,NULL);
- event_register_wimp_handler(-1,Wimp_EOpenWindow,open,(void*)id_block->self_id );
- event_register_wimp_handler(-1,Wimp_ECloseWindow,close,(void*)id_block->self_id );
- event_register_wimp_handler(id_block->self_id,Wimp_EMouseClick,button,(void*)id_block->self_id );
- event_register_message_handler(Message_DoneMyStuff,changed,(void*)id_block->self_id );
- event_register_message_handler(Message_UpdateArea,changed,(void*)id_block->self_id );
- event_register_message_handler(Message_WhosAbout,returned_message,(void*)id_block->self_id );
- event_register_message_handler(Message_HookMe,hookme_message,(void*)id_block->self_id );
- event_register_message_handler(Message_UnhookMe,unhookme_message,(void*)id_block->self_id );
- store_gadget("Window",id_block->self_id,0);
-
-
- handlers_attached=1;
- }
- break;
-
- case ProgInfo_ObjectClass:
-
- proginfo_set_version (0,id_block->self_id, "0.02 " __DATE__);
- break;
-
- case Menu_ObjectClass:
- if (strcmp(buffer,"Menu")==0)
-
- {
-
- toolmenu=id_block->self_id;
- event_register_toolbox_handler(id_block->self_id,0x300,hereIam,(void*)toolmenu);
- event_register_message_handler(Message_WhosAbout,returned_message,(void*)toolmenu );
-
- }
- if ((strcmp(buffer,"Main")==0) && (toolmenu!=0))
-
- {
- mainmenu=id_block->self_id;
- event_register_toolbox_handler(id_block->self_id,0x200,setup_menu,(void*)toolmenu);
- event_register_message_handler(Message_ImHere,WhosAbout_message,(void*)toolmenu);
- event_register_toolbox_handler(-1,0x500,do_the_free,(void*)mainmenu);
- event_register_toolbox_handler(-1,0x501,set_status,(void*)mainmenu);
- }
-
-
- break;
-
-
- }
-
-
- return 1;
- }
-
-
-
-
-
-
-
- void werr(int fatal, char* format, ...)
- {
- va_list va;
- _kernel_oserror e;
-
- e.errnum = 0;
- va_start(va, format);
- vsprintf(&e.errmess[0], format, va);
- va_end(va);
- wimp_report_error(&e, 0, "Processed",0,0,0);
- if (fatal) exit(1);
- }
-
-
-
- /*
- A mouse button has been pressed. If in place editing is active, pass
- the mouse click to the remote. Otherwise, look for the keyboard
- short cut.
- */
-
-
- int button(int event_code, WimpPollBlock *event,IdBlock *id_block,void *v)
- {
-
- ObjectId Oid;
-
- ToolboxEvent t;
- Oid=(ObjectId)v;
-
- if(HOOKED)
-
- {
-
- screen_to_sprite((WimpMouseClickEvent *)event);
-
- event->mouse_click.window_handle=hooked_w;
- event->mouse_click.icon_handle=-(0x414350);
- wimp_send_message(6,event,task[hooked_t][0],0,NULL);
-
-
- }
-
- else
- {
-
- if (akbd_pollsh() && akbd_pollctl())
- {
- t.hdr.size=16;
- t.hdr.event_code=0x200;
- t.hdr.flags=0;
- toolbox_raise_toolbox_event(0,mainmenu,NULL,&t);
- SHORTCUT=TRUE;
- }
-
- }
-
-
-
- return(TRUE);
- }
-
-
- /*
- Convert mouse coordinates to an ofset into the sprite.
- */
-
-
- void screen_to_sprite(WimpMouseClickEvent * c)
- {
-
- WimpGetWindowStateBlock state;
- int x,y;
-
-
-
- state.window_handle = c->window_handle;
- wimp_get_window_state(&state);
-
- x = (state.visible_area.xmin - state.xscroll);
- y = (state.visible_area.ymax - state.yscroll);
-
-
-
- c->mouse_x-=x;
- c->mouse_y=y-c->mouse_y;
- c->mouse_y=398-c->mouse_y;
-
-
-
- }
-
-
-
-
-
-
- int redraw_window(int event_code, WimpPollBlock *event,IdBlock *id_block,void *v)
- {
- int more,height,x,y;
- WimpGetWindowStateBlock state;
- WimpRedrawWindowBlock block;
- BBox extent;
-
-
- state.window_handle = event->redraw_window_request.window_handle;
- block.window_handle = event->redraw_window_request.window_handle;
-
- wimp_get_window_state(&state);
-
- window_get_extent(0, state.window_handle,&extent);
- height = extent.ymax -extent.ymin;
-
-
- x = (state.visible_area.xmin - state.xscroll);
- y = (state.visible_area.ymax - state.yscroll);
-
- wimp_redraw_window(&block,&more);
- while (more)
- {
- if(spblock!=NULL) sprite_put_in_current(spblock, &id, 0,x, y-398);
-
-
- wimp_get_rectangle (&block,&more);
- }
-
- return 1;
- }
-
-
-
-
- int update_window(int w_handle,BBox *box)
- {
- int more,x,y;
- WimpGetWindowStateBlock state;
- WimpRedrawWindowBlock block;
-
- state.window_handle = w_handle;
- wimp_get_window_state(&state);
-
- x = (state.visible_area.xmin - state.xscroll);
- y = (state.visible_area.ymax - state.yscroll);
-
- block.window_handle=w_handle;
- block.visible_area.xmin=box->xmin;
- block.visible_area.xmax=box->xmax;
- block.visible_area.ymin=box->ymin;
- block.visible_area.ymax=box->ymax;
-
-
-
- wimp_update_window(&block,&more);
- while (more)
- {
- if(spblock!=NULL) sprite_put_in_current(spblock, &id, 0,x, y-398);
-
-
- wimp_get_rectangle (&block,&more);
- }
-
- return 1;
- }
-
-
- void os_byte(int code,int * x,int *y)
- {
- _swi(OS_Byte,_IN(0)|_IN(1)|_IN(2)|_OUT(1)|_OUT(2),code,*x,*y,x,y);
- }
-
-
- int akbd_pollsh(void)
- {
- int x = -1;
- int y = 255;
- os_byte(129, &x, &y);
- return(x==255 && y==255);
- }
-
- int akbd_pollctl(void)
- {
- int x = -2;
- int y = 255;
- os_byte(129, &x, &y);
- return(x==255 && y==255);
- }
-
-
-
-
-