Search form

You are here

GetFile gadget and long path names

Submitted by OldFart on Wed, 2012-06-27 12:27

Blog Rate

up

29%

down

71%

GetFile gadget, long path names and how to deal with them
NOTE: A working example of how things look like is uploaded to OS4Depot under the name of 'longpathnames.lha'.
At one time we have probably all had to deal with path names that were considerably longer then the width of the getfile gadget. To ensure to have selected the proper file (or directory) a complete visual scan of the contents of the gadget was then the only option. That string had to be interpreted on the fly for the components it was made of. In critical situations, where only one component differed, this could easily lead to some degree of disaster.
This has kept me busy for some time now and I think I have found a neat solution to this problem enhancing the userfriendlyness of a GUI. That GUI need no longer be unwieldly wide to accomodate for a wide getfile gadget, which is plain ugly and very distracting. In stead there is only the need for a small gadget wide enough to hold the last part of the pathname string.
This latter requires the following tags be in effect for the gadget:

GETFILE_ReadOnly, TRUE,

GETFILE_FilePartOnly, TRUE,

The first one is required for the latter to be effective. The latter ensures that only the last part of a pathname sting is shown, requiring only a small gadget.
When the gadget is activated by clicking its button, the IDCMP-loop kicks in:

In this excerpt there is a call to function Convert_Path2HintInfo(), taking 2 arguments: File and Font. The first is a pointer to the pathname string, the latter is a pointer to a font-and-size string, which will be addressed later.
The function Convert_Path2HintInfo() looks like this:

More about the escape codes can be read in the AutoDocs: requester_cl.doc.
The octal \33 as specified there is 27 in decimal as used here (or 0x1B in hexadecimal) and denotes the start of an escaped sequence.
The font-and-size string used is something in this vein: "DejaVu Sans Mono.font/14". This will work perfectly well, but will ignore user's font preferences. This can be overcome by retrieving the system font for drawers as selected in the font preferences editor. This entry selects monospaced fonts only and is thereby perfectly suitable for our purposes.
NOTE: be sure to specify a monospace font or the results will look even more ugly then what we tryed to address!
The following function will supply you with a pointer to a struct TextAttr. It's argument is the specification to look for, in this case the value 1 will return the entry for the font used to show a drawer's contents.

Font-and-size string can now easily be created by using a function from the Utility library:
FontString = IUtility->ASPrintf("%s/%lu", ta->ta_Name, (ta->ta_YSize - 2));
Read the AutoDocs on this specific function!
In the function Retrieve_FontPrefs() a number of macro's is used, which I would also like to share as they are mightily handy.
Allocating a structure and clearing the entire structure with 0's:

NOTE WELL
Be sure to free (i.e. IExec->FreeVec()) both Hint, struct TextAttr and font-an-size string when the program is done.
Final note
The use of the above depicted way is not only limited to GetFile gadgets, but everywhere where a lengthy pathname CAN occur!