home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / timslib / OtherBits / c / SaveAs < prev    next >
Encoding:
Text File  |  1993-01-22  |  5.9 KB  |  233 lines

  1. /* SaveAs.c */
  2. /*                           
  3.  
  4.   #####                 #         #
  5.     #    #              #      #  #
  6.     #                   #         #
  7.     #   ##  ####   #### #     ##  ####
  8.     #    #  # # # #     #      #  #   #
  9.     #    #  # # #  ###  #      #  #   #
  10.     #    #  # # #     # #      #  #   #
  11.     #   ### # # # ####  ##### ### ####
  12.  
  13. -----------------------------------------------------------------------------
  14.  
  15. This is source for use with the 'DeskLib' Wimp C programming library for
  16. Risc OS. I currently use v1.04 of DeskLib.  This source is FreeWare, which
  17. means you can use it to write commercial applications, but you may not charge
  18. *in any way* for the distribution of this source.  I (Tim Browse) retain
  19. all copyright on this source.
  20.  
  21. This source is provided 'as is' and I offer no guarantees that is useful,
  22. bug-free, commented, that it will compile, or even that it exists.
  23.  
  24. If it breaks in half, you own both pieces.
  25.  
  26. All source © Tim Browse 1993
  27.  
  28. -----------------------------------------------------------------------------
  29.  
  30. */
  31.  
  32. /* Risc OS Lib */
  33. #include "werr.h"
  34.  
  35. /* DeskLib includes */
  36. #include "DeskLib.Wimp.h"
  37. #include "DeskLib.WimpSWIs.h"
  38. #include "DeskLib.Template.h"
  39. #include "DeskLib.Event.h"
  40. #include "DeskLib.Icon.h"
  41. #include "DeskLib.KeyCodes.h"
  42. #include "DeskLib.Window.h"
  43.  
  44. /* Timslib includes */
  45. #include "Timslib.Msgs.h"
  46. #include "Timslib.Menu.h"
  47.  
  48. /* Glazier includes */
  49. #include "GlzIcon.h"
  50. #include "XferSend.h"
  51. #include "FileIcon.h"
  52.  
  53. #include "SaveAs.h"
  54.  
  55. #define ICON_OK        0           /* OK action button */
  56. #define ICON_FILENAME  2           /* name field */
  57. #define ICON_FILEICON  3           /* icon to drag. */
  58.  
  59.  
  60. typedef struct
  61. {
  62.   int               filetype;
  63.   xfersend_saveproc saveproc;
  64.   void             *handle;
  65.   int               estsize;
  66.   char              filename[256];
  67. } saveas_block;
  68.  
  69. static saveas_block info;
  70.  
  71. static BOOL saveas_successful,
  72.             saveas_dbox_open;
  73.  
  74. static void SaveData(event_pollblock *event, window_handle window)
  75. {
  76.   int i;
  77.  
  78.   Icon_GetText(window, ICON_FILENAME, info.filename);
  79.  
  80.   /* Make sure we are using just the leafname */
  81.   i = strlen(info.filename) - 1;
  82.   while ((i > 0) && (info.filename[i] != '.'))
  83.     i--;
  84.  
  85.   /* If a dot found, convert filename to leafname */
  86.   if (info.filename[i] == '.')
  87.     strcpy(info.filename, info.filename + i + 1);
  88.  
  89.   /* Attempt to save the data */
  90.   saveas_successful =
  91.     XferSend(info.filetype, info.filename, info.estsize, info.saveproc,
  92.              event, info.handle);
  93.  
  94.  
  95.   /* Close the window if successful */
  96.   if (saveas_successful)
  97.   {
  98.     Wimp_CreateMenu((menu_block *) -1, 0, 0);
  99.     Window_Delete(window);
  100.     saveas_dbox_open = FALSE;
  101.   }
  102. }
  103.  
  104. static void TrySave(event_pollblock *event, window_handle window)
  105. {
  106.   int i = 0;
  107.   BOOL leafonly = TRUE;
  108.  
  109.   /* Get the filename user has entered */
  110.   Icon_GetText(window, ICON_FILENAME, info.filename);
  111.  
  112.   /* Look for a dot */
  113.   while (leafonly && info.filename[i] != '\0')
  114.   {
  115.     leafonly = info.filename[i] != '.';
  116.     i++;
  117.   }
  118.  
  119.   if (leafonly)
  120.     /* Dot not found - tell user how to save */
  121.     werr(FALSE, msgs_lookup("SaveAs.NoPath"));
  122.   else
  123.   {
  124.     saveas_successful = info.saveproc(info.filename, info.handle);
  125.  
  126.     /* Close the window if successful */
  127.     if (saveas_successful)
  128.     {
  129.       Wimp_CreateMenu((menu_block *) -1, 0, 0);
  130.       Window_Delete(window);
  131.       saveas_dbox_open = FALSE;
  132.     }
  133.   }
  134. }
  135.  
  136. static BOOL EventHandler(event_pollblock *event, void *handle)
  137. {
  138.   switch (event->type)
  139.   {
  140.     case event_BUTTON:
  141.       if (event->data.mouse.button.data.dragselect && 
  142.           (event->data.mouse.icon == ICON_FILEICON)) 
  143.         SaveData(event, event->data.mouse.window);
  144.       else
  145.       {
  146.         if (event->data.mouse.icon == ICON_OK)
  147.         {
  148.           /* Slab the OK button */
  149.           GlzIcon_Slab(&event->data.mouse);
  150.  
  151.           /* If filename entered is not a leafname, save data */
  152.           TrySave(event, event->data.mouse.window);
  153.         }   
  154.       }
  155.       return TRUE;
  156.  
  157.     case event_KEY:
  158.       /* If Return pressed, try to save data */
  159.       if (event->data.key.code == keycode_RETURN)
  160.         TrySave(event, event->data.key.caret.window);
  161.       return TRUE;
  162.  
  163.     case event_LOSECARET: /* Wimp doesn't give us a Close request */
  164.       saveas_dbox_open = FALSE;
  165.       Window_Delete(event->data.openblock.window);
  166.       return TRUE;
  167.  
  168.     default:
  169.       return FALSE; /* Pass on to next handler */
  170.   }
  171. }
  172.  
  173.  
  174. BOOL SaveAs(int filetype, char *name, int estsize,
  175.             xfersend_saveproc saveproc, void *handle)
  176. {
  177.   /* Create the Save As dialogue box */
  178.   window_handle window = Window_Create("xfer_send", 20);
  179.  
  180.   /* Set up the Dialogue box as required */
  181.   FileIcon(window, ICON_FILEICON, filetype);
  182.   Icon_SetText(window, ICON_FILENAME, name);
  183.  
  184.   /* Save the XferSend parameters */
  185.   info.filetype   = filetype;
  186.   info.estsize    = estsize;
  187.   info.saveproc   = saveproc;
  188.   info.handle     = handle;
  189.   strncpy(info.filename, name, 256);
  190.  
  191.   /* Attach event handler for saveas window events */
  192.   Event_Claim(event_ANY, window, event_ANY, EventHandler, (void *) 0);
  193.  
  194.   /* Work out where to show window - if it's hanging off a menu, open it as a
  195.      submenu, otherwise open as a menu.
  196.   */
  197.   if (menu_selection)
  198.   {
  199.     mouse_block ptr;
  200.     Wimp_GetPointerInfo(&ptr);
  201.  
  202.     /* Open as a menu under the pointer, but 64 OS units to the left of it, as
  203.        recommended in PRMs.
  204.     */
  205.     Wimp_CreateMenu((menu_block *) window, ptr.pos.x - 64, ptr.pos.y); 
  206.   }
  207.   else
  208.   {
  209.     /* Open as a submenu */
  210.     Wimp_CreateSubMenu((menu_block *) window, menu_submenupos.x, menu_submenupos.y);
  211.   }
  212.  
  213.   /* Keep processing events until user decides to do something */
  214.   saveas_successful = FALSE;
  215.   saveas_dbox_open  = TRUE;
  216.  
  217.   while (saveas_dbox_open)
  218.     Event_Poll();
  219.  
  220.   return saveas_successful;
  221. }
  222.  
  223. void SaveAs_ReadLeafname(char *name, int length)
  224. {
  225.   int i ;
  226.  
  227.   i = strlen(info.filename) - 1 ;
  228.   while ((i >= 0) && (info.filename[i] != '.'))
  229.     i--;
  230.  
  231.   strncpy(name, info.filename + i + 1, length);
  232. }
  233.