Thursday, January 5, 2012

How To Use the File Selection Popup

   
Written by Erez Wenger - January 2012

In this post we show how to make these file/directory popups appear, how to configure them and how to get the user entered information from them.

The file selection popup is a useful tool for dealing with files at the user level. Let's say you have a program which opens a file and then continues to process or display the data, in order for the user to load a different file on each run, you need him to specify which file he wants to load.

The file selection popup is an already designed popup dialog that lets the user select a file, placing that file's name inside a string for future use (a very common tool in windows applications).

Let's review a simple program that uses file selection popup :



In the program who's GUI is shown above there are 2 Command Buttons and 2 String controls.

The purpose of the "Load File" button is to call the File Selection Popup and put the name of the selected file on the "File Name" field.


The Code for the Load File button:

int CVICALLBACK loadf (int panel, int control, int event,
                                           void *callbackData, int eventData1, int eventData2)
{
       char fname[300];
       int sel_s;
       switch (event)
       {
              case EVENT_COMMIT:
                     sel_s = FileSelectPopup ("", "*.*", "", "Select a File",
                                                       VAL_LOAD_BUTTON, 0, 0, 1, 0, fname);
                     if (sel_s)
                           SetCtrlVal (panelHandle, PANEL_STRING, fname);
                    break;
       }
       return 0;
}

The first line of code is the FileSelectPopup fuction which prompts the user to select a file, storing its name in a string (which one? see details below), the following two lines of code places the content of the above string on screen if the user has selected a valid file (and did not cancel the selection process).

The FileSelectPopup consists of some parameters which are shown in the function panel:



The important parameters are:

File Type List - specifies what types of files can be loaded/saved

Title - the title of the popup panel

Button Label - if it's a Save or Load button (it is important to choose the correct label since it also determines whether the user can enter a new file name or not)

Path Name - a string variable that will hold the name of the selected file

Selection Status - the integer value returned by the function - a positive value if the file selection is valid or else 0


The popup panel:



The other 2 controls of the program (the first image above) - the "Load Directory" button and the "Directory Name" string control demonstrate another similar operation of directory selection. The directory selection popup is the same as the file selection popup, but in this case used for the selection of directories. Here is the code for the operation, and I believe you can easily figure out how to fill in the arguments of the DirSelectPopup function.

The Code for the Load Directory button:

int CVICALLBACK loadd (int panel, int control, int event,
                                            void *callbackData, int eventData1, int eventData2)
{
       char dname[300];
       int sel_s;
       switch (event)
       {
              case EVENT_COMMIT:
                     sel_s = DirSelectPopup ("", "Select Directory", 1, 1, dname);
                     if (sel_s)
                           SetCtrlVal (panelHandle, PANEL_STRING_2, dname);
                    break;
       }
       return 0;
 }

The Popup Panel:



The Result:


1 comment: