home *** CD-ROM | disk | FTP | other *** search
- /* SaveAs.c */
- /*
-
- ##### # #
- # # # # #
- # # #
- # ## #### #### # ## ####
- # # # # # # # # # #
- # # # # # ### # # # #
- # # # # # # # # # #
- # ### # # # #### ##### ### ####
-
- -----------------------------------------------------------------------------
-
- This is source for use with the 'DeskLib' Wimp C programming library for
- Risc OS. I currently use v1.04 of DeskLib. This source is FreeWare, which
- means you can use it to write commercial applications, but you may not charge
- *in any way* for the distribution of this source. I (Tim Browse) retain
- all copyright on this source.
-
- This source is provided 'as is' and I offer no guarantees that is useful,
- bug-free, commented, that it will compile, or even that it exists.
-
- If it breaks in half, you own both pieces.
-
- All source © Tim Browse 1993
-
- -----------------------------------------------------------------------------
-
- */
-
- /* Risc OS Lib */
- #include "werr.h"
-
- /* DeskLib includes */
- #include "DeskLib.Wimp.h"
- #include "DeskLib.WimpSWIs.h"
- #include "DeskLib.Template.h"
- #include "DeskLib.Event.h"
- #include "DeskLib.Icon.h"
- #include "DeskLib.KeyCodes.h"
- #include "DeskLib.Window.h"
-
- /* Timslib includes */
- #include "Timslib.Msgs.h"
- #include "Timslib.Menu.h"
-
- /* Glazier includes */
- #include "GlzIcon.h"
- #include "XferSend.h"
- #include "FileIcon.h"
-
- #include "SaveAs.h"
-
- #define ICON_OK 0 /* OK action button */
- #define ICON_FILENAME 2 /* name field */
- #define ICON_FILEICON 3 /* icon to drag. */
-
-
- typedef struct
- {
- int filetype;
- xfersend_saveproc saveproc;
- void *handle;
- int estsize;
- char filename[256];
- } saveas_block;
-
- static saveas_block info;
-
- static BOOL saveas_successful,
- saveas_dbox_open;
-
- static void SaveData(event_pollblock *event, window_handle window)
- {
- int i;
-
- Icon_GetText(window, ICON_FILENAME, info.filename);
-
- /* Make sure we are using just the leafname */
- i = strlen(info.filename) - 1;
- while ((i > 0) && (info.filename[i] != '.'))
- i--;
-
- /* If a dot found, convert filename to leafname */
- if (info.filename[i] == '.')
- strcpy(info.filename, info.filename + i + 1);
-
- /* Attempt to save the data */
- saveas_successful =
- XferSend(info.filetype, info.filename, info.estsize, info.saveproc,
- event, info.handle);
-
-
- /* Close the window if successful */
- if (saveas_successful)
- {
- Wimp_CreateMenu((menu_block *) -1, 0, 0);
- Window_Delete(window);
- saveas_dbox_open = FALSE;
- }
- }
-
- static void TrySave(event_pollblock *event, window_handle window)
- {
- int i = 0;
- BOOL leafonly = TRUE;
-
- /* Get the filename user has entered */
- Icon_GetText(window, ICON_FILENAME, info.filename);
-
- /* Look for a dot */
- while (leafonly && info.filename[i] != '\0')
- {
- leafonly = info.filename[i] != '.';
- i++;
- }
-
- if (leafonly)
- /* Dot not found - tell user how to save */
- werr(FALSE, msgs_lookup("SaveAs.NoPath"));
- else
- {
- saveas_successful = info.saveproc(info.filename, info.handle);
-
- /* Close the window if successful */
- if (saveas_successful)
- {
- Wimp_CreateMenu((menu_block *) -1, 0, 0);
- Window_Delete(window);
- saveas_dbox_open = FALSE;
- }
- }
- }
-
- static BOOL EventHandler(event_pollblock *event, void *handle)
- {
- switch (event->type)
- {
- case event_BUTTON:
- if (event->data.mouse.button.data.dragselect &&
- (event->data.mouse.icon == ICON_FILEICON))
- SaveData(event, event->data.mouse.window);
- else
- {
- if (event->data.mouse.icon == ICON_OK)
- {
- /* Slab the OK button */
- GlzIcon_Slab(&event->data.mouse);
-
- /* If filename entered is not a leafname, save data */
- TrySave(event, event->data.mouse.window);
- }
- }
- return TRUE;
-
- case event_KEY:
- /* If Return pressed, try to save data */
- if (event->data.key.code == keycode_RETURN)
- TrySave(event, event->data.key.caret.window);
- return TRUE;
-
- case event_LOSECARET: /* Wimp doesn't give us a Close request */
- saveas_dbox_open = FALSE;
- Window_Delete(event->data.openblock.window);
- return TRUE;
-
- default:
- return FALSE; /* Pass on to next handler */
- }
- }
-
-
- BOOL SaveAs(int filetype, char *name, int estsize,
- xfersend_saveproc saveproc, void *handle)
- {
- /* Create the Save As dialogue box */
- window_handle window = Window_Create("xfer_send", 20);
-
- /* Set up the Dialogue box as required */
- FileIcon(window, ICON_FILEICON, filetype);
- Icon_SetText(window, ICON_FILENAME, name);
-
- /* Save the XferSend parameters */
- info.filetype = filetype;
- info.estsize = estsize;
- info.saveproc = saveproc;
- info.handle = handle;
- strncpy(info.filename, name, 256);
-
- /* Attach event handler for saveas window events */
- Event_Claim(event_ANY, window, event_ANY, EventHandler, (void *) 0);
-
- /* Work out where to show window - if it's hanging off a menu, open it as a
- submenu, otherwise open as a menu.
- */
- if (menu_selection)
- {
- mouse_block ptr;
- Wimp_GetPointerInfo(&ptr);
-
- /* Open as a menu under the pointer, but 64 OS units to the left of it, as
- recommended in PRMs.
- */
- Wimp_CreateMenu((menu_block *) window, ptr.pos.x - 64, ptr.pos.y);
- }
- else
- {
- /* Open as a submenu */
- Wimp_CreateSubMenu((menu_block *) window, menu_submenupos.x, menu_submenupos.y);
- }
-
- /* Keep processing events until user decides to do something */
- saveas_successful = FALSE;
- saveas_dbox_open = TRUE;
-
- while (saveas_dbox_open)
- Event_Poll();
-
- return saveas_successful;
- }
-
- void SaveAs_ReadLeafname(char *name, int length)
- {
- int i ;
-
- i = strlen(info.filename) - 1 ;
- while ((i >= 0) && (info.filename[i] != '.'))
- i--;
-
- strncpy(name, info.filename + i + 1, length);
- }
-