home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / program / d / gstream / c / savebox < prev    next >
Encoding:
Text File  |  1993-05-08  |  9.6 KB  |  318 lines

  1. /*************************************************************
  2. *                                                            *
  3. *    Module: savebox.c (part of the gstream package)         *
  4. *                                                            *
  5. *   Version: 0.01                                            *
  6. *                                                            *
  7. *    Author: Simon Proven                                    *
  8. *                                                            *
  9. *     Owner:  Simon Proven 1993                             *
  10. *                                                            *
  11. *   Purpose: To provide a saveas window that uses gstream    *
  12. *                                                            *
  13. *      Uses: wimp, wimpt, win, gstream, osstream, ramstream, *
  14. *            string, template, event, fileicon, bbc,         *
  15. *            xferstream, werr                                *
  16. *                                                            *
  17. * Conditions:                                                *
  18. *             You can produce and sell executable code from  *
  19. *             this module freely.  The source code may be    *
  20. *             distributed freely, but not for profit.        *
  21. *                                                            *
  22. *             If you like these modules alot, and use them   *
  23. *             in your own programs, feel free to thank me    *
  24. *             with cash - 10 or more will get you a disc    *
  25. *             with the latest versions and new stream types. *
  26. *                                                            *
  27. * Disclaimer:                                                *
  28. *             This software is supplied in good faith, but I *
  29. *             cannot accept liability for any loss incurred  *
  30. *             through the use or inability to use any part   *
  31. *             of this software.                              *
  32. *                                                            *
  33. * How to contact me:                                         *
  34. *                                                            *
  35. * email:                      snail mail:                    *
  36. * sproven@cs.strath.ac.uk     Simon Proven,                  *
  37. *                             Castle Cottage,                *
  38. *                             Portencross,                   *
  39. *                             West Kilbride,                 *
  40. *                             KA23 9QA                       *
  41. *                             SCOTLAND                       *
  42. *                                                            *
  43. * Tel.  (+44) 294 829 721                                    *
  44. *                                                            *
  45. *************************************************************/
  46.  
  47. #include "wimp.h"
  48. #include "wimpt.h"
  49. #include "win.h"
  50. #include "gstream.h"
  51. #include "osstream.h"
  52. #include "ramstream.h"
  53. #include "string.h"
  54. #include "template.h"
  55. #include "event.h"
  56. #include "fileicon.h"
  57. #include "bbc.h"
  58. #include "savebox.h"
  59. #include "xferstream.h"
  60. #include "werr.h"
  61.  
  62. #define file_icon 3
  63. #define name_icon 2
  64. #define go_icon 0
  65.  
  66. static    char    *savebox__filename;
  67. static  char    savebox__leafname[256];
  68. static    gstream    *savebox__stream;
  69. static    int    savebox_state=1;
  70. static    wimp_w    savebox_window;
  71. static    int    savebox_filetype;
  72. static    int    savebox_estsize;
  73. static    int    *savebox_safeptr;
  74.  
  75. static int validpath(char *path)
  76. {        
  77.     if (strchr(path,':')==NULL && strchr(path,'.')==NULL)
  78.         return FALSE;
  79.     else
  80.         return TRUE;
  81. }
  82.  
  83. static char *make_leaf(char *path)
  84. {
  85.     char *pos;
  86.  
  87.     if ((pos=strrchr(path,'.'))!=NULL)
  88.         return(pos+1);
  89.     else if ((pos=strrchr(path,':'))!=NULL)
  90.         return(pos+1);
  91.     else
  92.         return(path);
  93. }
  94.  
  95. static int savebox_draghandler(wimp_eventstr *e, void *handle)
  96. {
  97.     wimp_mousestr m;
  98.  
  99.     switch (e->e)
  100.     {
  101.         case wimp_EUSERDRAG:
  102.             wimp_get_point_info(&m);
  103.             if (m.w!=-1)
  104.             {
  105.                 savebox_state=1;
  106.                 if (gsopenxfersend(    savebox__leafname,
  107.                             savebox_filetype,
  108.                             savebox_estsize,
  109.                             savebox_safeptr,
  110.                             savebox__stream))
  111.                 {
  112.                     win_remove_unknown_event_processor(savebox_draghandler,handle);
  113.                     wimp_delete_wind(savebox_window);                    
  114.                     if (*savebox_safeptr)
  115.                         strcpy(savebox__filename,savebox__leafname);
  116.                 }
  117.                 else
  118.                     savebox_state=0;
  119.             }
  120.             return TRUE;
  121.         default:
  122.             return FALSE;
  123.     }
  124. }
  125.  
  126. /* at Uni we get taught to make our functions no more than a
  127.  * page high.  Bollox! :-)
  128.  */
  129.  
  130. static void savebox_handler(wimp_eventstr *e, void *handle)
  131. {
  132.     wimp_icon iconinfo;
  133.     switch (e->e)
  134.     {
  135.         case wimp_EBUT:
  136.             /* mouse clicked */
  137.             switch (e->data.but.m.i)
  138.             {
  139.                 case go_icon:
  140.                     /* here we have a click on OK */
  141.                     wimp_get_icon_info(savebox_window,name_icon,&iconinfo);
  142.                     if (validpath(iconinfo.data.indirecttext.buffer))
  143.                     {
  144.                         if (gsopenosw(iconinfo.data.indirecttext.buffer,savebox__stream))
  145.                         {
  146.                             /* now we have successfully opened the file so
  147.                              * we must close the window and go byebye
  148.                              */
  149.                             wimp_delete_wind(savebox_window);
  150.                             savebox_state=1;
  151.                             strcpy(savebox__filename,iconinfo.data.indirecttext.buffer);
  152.                             setfiletype(savebox__filename,savebox_filetype);
  153.                         }
  154.                         else
  155.                         {
  156.                             wimp_delete_wind(savebox_window);
  157.                             savebox_state=2;
  158.                         }
  159.                     }
  160.                     else
  161.                         werr(FALSE,"To save, drag the icon to a directory viewer");
  162.                     break;
  163.                 case file_icon:
  164.                     /* here there is a click or drag on the file icon */
  165.                     if (e->data.but.m.bbits & (16*4))
  166.                     {
  167.                         wimp_dragstr    d;
  168.                         wimp_wstate    s;
  169.                         wimp_icon    i;
  170.                         int        x_limit=bbc_vduvar(bbc_XWindLimit)<<bbc_vduvar(bbc_XEigFactor),
  171.                                 y_limit=bbc_vduvar(bbc_YWindLimit)<<bbc_vduvar(bbc_YEigFactor);
  172.                         wimpt_complain(wimp_get_wind_state(savebox_window,&s));
  173.                         wimpt_complain(wimp_get_icon_info(savebox_window,file_icon,&i));
  174.                         /* the user just dragged */
  175.                         d.type=wimp_USER_FIXED;
  176.                         d.box.x0=s.o.box.x0-s.o.x+i.box.x0;
  177.                         d.box.y0=s.o.box.y1-s.o.y+i.box.y0;
  178.                         d.box.x1=s.o.box.x0-s.o.x+i.box.x1;
  179.                         d.box.y1=s.o.box.y1-s.o.y+i.box.y1;
  180.                         d.parent.x0=0;
  181.                         d.parent.y0=0;
  182.                         d.parent.x1=x_limit;
  183.                         d.parent.y1=y_limit;
  184.                         wimp_drag_box(&d);
  185.                         wimp_get_icon_info(savebox_window,name_icon,&iconinfo);
  186.                         strcpy(savebox__leafname,make_leaf(iconinfo.data.indirecttext.buffer));
  187.                         win_add_unknown_event_processor(savebox_draghandler,NULL);
  188.                     }
  189.                     break;
  190.             }
  191.             break;
  192.         case wimp_ECLOSE:
  193.             /* window closed */
  194.             wimp_delete_wind(savebox_window);
  195.             savebox_state=2;
  196.             break;
  197.         case wimp_EOPEN:
  198.             /* window opened in new posn */
  199.             wimp_open_wind(&(e->data.o));
  200.             break;
  201.         case wimp_EKEY:
  202.             /* key pressed */
  203.             switch(e->data.key.chcode)
  204.             {
  205.                 case 13:
  206.                     /* here we have a return pressed */
  207.                     wimp_get_icon_info(savebox_window,name_icon,&iconinfo);
  208.                     if (validpath(iconinfo.data.indirecttext.buffer))
  209.                     {
  210.                         if (gsopenosw(iconinfo.data.indirecttext.buffer,savebox__stream))
  211.                         {
  212.                             /* now we have successfully opened the file so
  213.                              * we must close the window and go byebye
  214.                              */
  215.                             wimp_delete_wind(savebox_window);
  216.                             savebox_state=1;
  217.                             strcpy(savebox__filename,iconinfo.data.indirecttext.buffer);
  218.                             setfiletype(savebox__filename,savebox_filetype);
  219.                         }
  220.                         else
  221.                         {
  222.                             wimp_delete_wind(savebox_window);
  223.                             savebox_state=2;
  224.                         }
  225.                     }
  226.                     else
  227.                         werr(FALSE,"To save, drag the icon to a directory viewer");
  228.                     break;
  229.                 case 27:
  230.                     wimp_delete_wind(savebox_window);
  231.                     savebox_state=2;
  232.                     break;
  233.                 default:
  234.                     wimp_processkey(e->data.key.chcode);
  235.             }
  236.             break;
  237.     }
  238. }
  239.  
  240. /* this function will open a save box, either under the mouse pointer
  241.  * or off a submenu arrow (if the last event was a message_MenuWarning
  242.  * then the submenu will be used)
  243.  *
  244.  * Only one savebox at a time is supported (if you want more than one
  245.  * savebox at a time then you're on your own mate!)
  246.  *
  247.  * If the function returns TRUE then a stream has been opened.  name
  248.  * will now contain the new pathname if the file was saved to a safe
  249.  * place.  If the file was saved to a safe place, *safeptr will be TRUE,
  250.  * otherwise it will be FALSE.
  251.  */
  252.  
  253. int savebox(    char    *filename,
  254.         int    estsize,
  255.         int    type,
  256.         int    *safeptr,
  257.         gstream *stream)
  258. {
  259.     wimp_wstate    state;
  260.     template    *temp;
  261.     wimp_icon    iconinfo;
  262.     wimp_eventstr    *e;
  263.     int        x,y;
  264.     wimp_caretstr    caret;
  265.  
  266.     temp=template_copy(template_find("xfer_send"));
  267.     e=wimpt_last_event();
  268.     if ((e->e==wimp_ESEND || e->e==wimp_ESENDWANTACK) && e->data.msg.hdr.action==wimp_MMENUWARN )
  269.     {
  270.         x=e->data.msg.data.words[1];
  271.         y=e->data.msg.data.words[2];
  272.     }
  273.     else
  274.     {
  275.         wimp_mousestr m;
  276.         wimp_get_point_info(&m);
  277.         x=m.x-64;
  278.         y=m.y+64;
  279.         if (y<192)
  280.             y=192;
  281.         /* add a close icon since clicking elsewhere or returning
  282.          * to the parent menu will not close the save box
  283.          */
  284.         temp->window.flags=temp->window.flags | wimp_WQUIT;    
  285.     }
  286.     wimp_create_wind(&(temp->window),&savebox_window);
  287.     wimp_get_wind_state(savebox_window,&state);
  288.     savebox__stream=stream;
  289.     savebox_filetype=type;
  290.     savebox_estsize=estsize;
  291.     savebox_safeptr=safeptr;
  292.     fileicon(savebox_window,file_icon,type);
  293.     state.o.behind=-1;
  294.     wimp_get_icon_info(savebox_window,name_icon,&iconinfo);
  295.     strcpy(iconinfo.data.indirecttext.buffer,filename);
  296.     savebox__filename=filename;
  297.     state.o.box.x1=state.o.box.x1-state.o.box.x0+x;
  298.     state.o.box.y0=state.o.box.y0-state.o.box.y1+y;    
  299.     state.o.box.x0=x;
  300.     state.o.box.y1=y;
  301.     wimp_open_wind(&state.o);
  302.  
  303.     caret.w=savebox_window;
  304.     caret.i=name_icon;
  305.     caret.height=-1;
  306.     caret.index=strlen(iconinfo.data.indirecttext.buffer);
  307.     wimp_set_caret_pos(&caret);
  308.  
  309.     win_register_event_handler(savebox_window,savebox_handler,NULL);
  310.     savebox_state=0;
  311.     do{event_process();}while(savebox_state==0);
  312.     if (savebox_state==1)
  313.         return(TRUE);
  314.     else
  315.         return(FALSE);
  316. }
  317.  
  318.