home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************
- * *
- * Module: savebox.c (part of the gstream package) *
- * *
- * Version: 0.01 *
- * *
- * Author: Simon Proven *
- * *
- * Owner: Simon Proven 1993 *
- * *
- * Purpose: To provide a saveas window that uses gstream *
- * *
- * Uses: wimp, wimpt, win, gstream, osstream, ramstream, *
- * string, template, event, fileicon, bbc, *
- * xferstream, werr *
- * *
- * Conditions: *
- * You can produce and sell executable code from *
- * this module freely. The source code may be *
- * distributed freely, but not for profit. *
- * *
- * If you like these modules alot, and use them *
- * in your own programs, feel free to thank me *
- * with cash - 10 or more will get you a disc *
- * with the latest versions and new stream types. *
- * *
- * Disclaimer: *
- * This software is supplied in good faith, but I *
- * cannot accept liability for any loss incurred *
- * through the use or inability to use any part *
- * of this software. *
- * *
- * How to contact me: *
- * *
- * email: snail mail: *
- * sproven@cs.strath.ac.uk Simon Proven, *
- * Castle Cottage, *
- * Portencross, *
- * West Kilbride, *
- * KA23 9QA *
- * SCOTLAND *
- * *
- * Tel. (+44) 294 829 721 *
- * *
- *************************************************************/
-
- #include "wimp.h"
- #include "wimpt.h"
- #include "win.h"
- #include "gstream.h"
- #include "osstream.h"
- #include "ramstream.h"
- #include "string.h"
- #include "template.h"
- #include "event.h"
- #include "fileicon.h"
- #include "bbc.h"
- #include "savebox.h"
- #include "xferstream.h"
- #include "werr.h"
-
- #define file_icon 3
- #define name_icon 2
- #define go_icon 0
-
- static char *savebox__filename;
- static char savebox__leafname[256];
- static gstream *savebox__stream;
- static int savebox_state=1;
- static wimp_w savebox_window;
- static int savebox_filetype;
- static int savebox_estsize;
- static int *savebox_safeptr;
-
- static int validpath(char *path)
- {
- if (strchr(path,':')==NULL && strchr(path,'.')==NULL)
- return FALSE;
- else
- return TRUE;
- }
-
- static char *make_leaf(char *path)
- {
- char *pos;
-
- if ((pos=strrchr(path,'.'))!=NULL)
- return(pos+1);
- else if ((pos=strrchr(path,':'))!=NULL)
- return(pos+1);
- else
- return(path);
- }
-
- static int savebox_draghandler(wimp_eventstr *e, void *handle)
- {
- wimp_mousestr m;
-
- switch (e->e)
- {
- case wimp_EUSERDRAG:
- wimp_get_point_info(&m);
- if (m.w!=-1)
- {
- savebox_state=1;
- if (gsopenxfersend( savebox__leafname,
- savebox_filetype,
- savebox_estsize,
- savebox_safeptr,
- savebox__stream))
- {
- win_remove_unknown_event_processor(savebox_draghandler,handle);
- wimp_delete_wind(savebox_window);
- if (*savebox_safeptr)
- strcpy(savebox__filename,savebox__leafname);
- }
- else
- savebox_state=0;
- }
- return TRUE;
- default:
- return FALSE;
- }
- }
-
- /* at Uni we get taught to make our functions no more than a
- * page high. Bollox! :-)
- */
-
- static void savebox_handler(wimp_eventstr *e, void *handle)
- {
- wimp_icon iconinfo;
- switch (e->e)
- {
- case wimp_EBUT:
- /* mouse clicked */
- switch (e->data.but.m.i)
- {
- case go_icon:
- /* here we have a click on OK */
- wimp_get_icon_info(savebox_window,name_icon,&iconinfo);
- if (validpath(iconinfo.data.indirecttext.buffer))
- {
- if (gsopenosw(iconinfo.data.indirecttext.buffer,savebox__stream))
- {
- /* now we have successfully opened the file so
- * we must close the window and go byebye
- */
- wimp_delete_wind(savebox_window);
- savebox_state=1;
- strcpy(savebox__filename,iconinfo.data.indirecttext.buffer);
- setfiletype(savebox__filename,savebox_filetype);
- }
- else
- {
- wimp_delete_wind(savebox_window);
- savebox_state=2;
- }
- }
- else
- werr(FALSE,"To save, drag the icon to a directory viewer");
- break;
- case file_icon:
- /* here there is a click or drag on the file icon */
- if (e->data.but.m.bbits & (16*4))
- {
- wimp_dragstr d;
- wimp_wstate s;
- wimp_icon i;
- int x_limit=bbc_vduvar(bbc_XWindLimit)<<bbc_vduvar(bbc_XEigFactor),
- y_limit=bbc_vduvar(bbc_YWindLimit)<<bbc_vduvar(bbc_YEigFactor);
- wimpt_complain(wimp_get_wind_state(savebox_window,&s));
- wimpt_complain(wimp_get_icon_info(savebox_window,file_icon,&i));
- /* the user just dragged */
- d.type=wimp_USER_FIXED;
- d.box.x0=s.o.box.x0-s.o.x+i.box.x0;
- d.box.y0=s.o.box.y1-s.o.y+i.box.y0;
- d.box.x1=s.o.box.x0-s.o.x+i.box.x1;
- d.box.y1=s.o.box.y1-s.o.y+i.box.y1;
- d.parent.x0=0;
- d.parent.y0=0;
- d.parent.x1=x_limit;
- d.parent.y1=y_limit;
- wimp_drag_box(&d);
- wimp_get_icon_info(savebox_window,name_icon,&iconinfo);
- strcpy(savebox__leafname,make_leaf(iconinfo.data.indirecttext.buffer));
- win_add_unknown_event_processor(savebox_draghandler,NULL);
- }
- break;
- }
- break;
- case wimp_ECLOSE:
- /* window closed */
- wimp_delete_wind(savebox_window);
- savebox_state=2;
- break;
- case wimp_EOPEN:
- /* window opened in new posn */
- wimp_open_wind(&(e->data.o));
- break;
- case wimp_EKEY:
- /* key pressed */
- switch(e->data.key.chcode)
- {
- case 13:
- /* here we have a return pressed */
- wimp_get_icon_info(savebox_window,name_icon,&iconinfo);
- if (validpath(iconinfo.data.indirecttext.buffer))
- {
- if (gsopenosw(iconinfo.data.indirecttext.buffer,savebox__stream))
- {
- /* now we have successfully opened the file so
- * we must close the window and go byebye
- */
- wimp_delete_wind(savebox_window);
- savebox_state=1;
- strcpy(savebox__filename,iconinfo.data.indirecttext.buffer);
- setfiletype(savebox__filename,savebox_filetype);
- }
- else
- {
- wimp_delete_wind(savebox_window);
- savebox_state=2;
- }
- }
- else
- werr(FALSE,"To save, drag the icon to a directory viewer");
- break;
- case 27:
- wimp_delete_wind(savebox_window);
- savebox_state=2;
- break;
- default:
- wimp_processkey(e->data.key.chcode);
- }
- break;
- }
- }
-
- /* this function will open a save box, either under the mouse pointer
- * or off a submenu arrow (if the last event was a message_MenuWarning
- * then the submenu will be used)
- *
- * Only one savebox at a time is supported (if you want more than one
- * savebox at a time then you're on your own mate!)
- *
- * If the function returns TRUE then a stream has been opened. name
- * will now contain the new pathname if the file was saved to a safe
- * place. If the file was saved to a safe place, *safeptr will be TRUE,
- * otherwise it will be FALSE.
- */
-
- int savebox( char *filename,
- int estsize,
- int type,
- int *safeptr,
- gstream *stream)
- {
- wimp_wstate state;
- template *temp;
- wimp_icon iconinfo;
- wimp_eventstr *e;
- int x,y;
- wimp_caretstr caret;
-
- temp=template_copy(template_find("xfer_send"));
- e=wimpt_last_event();
- if ((e->e==wimp_ESEND || e->e==wimp_ESENDWANTACK) && e->data.msg.hdr.action==wimp_MMENUWARN )
- {
- x=e->data.msg.data.words[1];
- y=e->data.msg.data.words[2];
- }
- else
- {
- wimp_mousestr m;
- wimp_get_point_info(&m);
- x=m.x-64;
- y=m.y+64;
- if (y<192)
- y=192;
- /* add a close icon since clicking elsewhere or returning
- * to the parent menu will not close the save box
- */
- temp->window.flags=temp->window.flags | wimp_WQUIT;
- }
- wimp_create_wind(&(temp->window),&savebox_window);
- wimp_get_wind_state(savebox_window,&state);
- savebox__stream=stream;
- savebox_filetype=type;
- savebox_estsize=estsize;
- savebox_safeptr=safeptr;
- fileicon(savebox_window,file_icon,type);
- state.o.behind=-1;
- wimp_get_icon_info(savebox_window,name_icon,&iconinfo);
- strcpy(iconinfo.data.indirecttext.buffer,filename);
- savebox__filename=filename;
- state.o.box.x1=state.o.box.x1-state.o.box.x0+x;
- state.o.box.y0=state.o.box.y0-state.o.box.y1+y;
- state.o.box.x0=x;
- state.o.box.y1=y;
- wimp_open_wind(&state.o);
-
- caret.w=savebox_window;
- caret.i=name_icon;
- caret.height=-1;
- caret.index=strlen(iconinfo.data.indirecttext.buffer);
- wimp_set_caret_pos(&caret);
-
- win_register_event_handler(savebox_window,savebox_handler,NULL);
- savebox_state=0;
- do{event_process();}while(savebox_state==0);
- if (savebox_state==1)
- return(TRUE);
- else
- return(FALSE);
- }
-
-