C for Yourself - part 31

Steve Mumford explains the correct naming of files for saving

Last issue I started to describe the principles of saving a file by dragging an icon, and wound up with an application that performed the dragging part, but didn't really care about what happened to the icon after that. In this issue, I've expanded the library so it's possible to determine where a dragged icon was dropped, and from there decide what filename you should use in order to save it in the right place.

Because a dragging operation can span a number of Wimp_Poll cycles, it's important to keep track of the nature of the drag so the program can decide how it treats any User_DragBox messages. I've done this with a simple global variable. It's not particularly vital if your application only uses one drag box, most often in its save window, but if more than one exists, things could get particularly confusing without it.

There's a new data structure in this version of AULib, and it's used to hold information about the position of the mouse:

typedef struct pointer_data
  long int mouse_x;
  long int mouse_y;
  long int button_state;
  long int win_handle;
  long int icon_handle;
} pointer_data;

Once you've declared a structure of this type, you can call au_getpointerinfo() to grab the current state of the mouse - this information can then be used for a variety of purposes. In our case we're going to paraphrase the data and send a Data_Save message to the filer. As show below, the au_getpointerinfo() function takes an address of a structure as its parameter, so that it can move all the resultant information into it directly:

pointer_data pointer_info;


We then check to see whether we were in the middle of a filesave drag, by using the global variable mentioned earlier. Because the user should have typed a leafname (the final bit of the full pathname of a file, such as TextFile in the example ADFS::0.$.Docs.TextFile) into the field below the draggable icon, we must interrogate it and retrieve the string before we go any further.

I didn't have a mnemonic for the number of thid particular writable icon up till now, si I #defined one and included it with the rest at the top of the file - in this way, if you change the numbering of your window templates, it's very easy to reflect those changes in the C sourcecode. An example from the disc is shown below:

char temp_name[256];

au_icon_get_text(temp_name, win_dta[2].win_handle, SAVE_FILENAME_ICON);

Now we have the leafname, we can go ahead and send a message to the WIMP filer, using the new function au_datasave(). This takes four parameters, which are in order as follows - the first is a full pointer_data structure, the second is an estimated file size (so the filer can check whether it's likely to fit on a particular disc without trying), the third is a filetype and the fourth is the leafname.

au_datasave(pointer_info, 1024, 0xFFF, temp_name);

At this stage, we have to sit back and wait again; the Filer will receive our message and if everything goes according to plan, it will return a DataSaveAck message. To complete the operation, we just wait until we're returned a value of 17, 18 or 19 from the Wimp_Poll loop, check for the DataSaveAck number and read a filename from the poll block - the name starts at byte 44 and the string is zero-terminated as normal. Just to act as a demonstration, the example program on the cover disc will pop up an information window giving the full pathname of the prospective file as soon as the user drops the save icon in an appropriate window.

This month, another program has been included on the cover disc for you to examine. It's called Acronyms and it's written by Gareth Duncan, it uses the Acorn User library for some of the features discussed in the past few months.

Acronyms is a little utility of use to anyonw who wants to decipher any Usenet or e-mail messages peppered with those inconceivable abbreviations such as ROTFL, IMNSHO or even TLA. The dictionaries also contain all manner of useful computer acronyms, so if you've ever wondered about POPs, SIMMs or TAOS, Gareth's utility has all the answers. The code is nicely commented, so take a look and follow it through.

Source: Acorn User - 175 - December 1996
Publication: Acorn User
Contributor: Steve Mumford