Next: , Previous: , Up: Classes   [Index]


X11FileSelectDialog

X11FileSelectDialog Class

A X11FileSelectDialog object displays a dialog box with a list of files in the current directory, a preselected target directory, or a subdirectory that a user selects from the current directory’s entries.

The class allows users to select multiple entries by holding either the Shift or Ctrl keys while selecting items. The class provides a number of methods to retrieve the number of items that the user selects, the items’ text, and text for a new file that the user enters, if any. These methods are described below. See Retrieving-Selection-Information.

The X11FileSelectDialog class can also display a text entry box (from X11TextEntryPane class) where users can enter new file names.

Here is an example program that pops up a X11FileSelectDialog window.


/* -*-c-*- */

/* 
 *   fileselectbox.ca, a X11FileSelectDialog demonstration. 
 *
 *     To build:
 * 
 *       ctcc -x fileselectbox.ca -o fileselectbox
 *
 *     You can optionally buidl the dialog with a X11TextEntryPane
 *     so you can enter filenames by typing.  For a full description
 *     of the X11FileSelectDialog class, refer to the Ctalk Language
 *     Reference.
 */

/*
 *  Uncomment the following #define if you want to build the dialog
 *  with a X11TextEntryPane for creating new file paths.
 */
/* #define FILENAME_ENTRY_BOX */

#include <ctalk/ctalkdefs.h>

int main (void) {
  X11Pane new mainWindow;
  X11PaneDispatcher new dispatcher;
  X11ButtonPane new button;
  X11LabelPane new label;
  X11FileSelectDialog new fileSelect;
  InputEvent new e;
  Integer new i;
  List new itemTextOut, itemNOut;
  String new itemText, itemN;

  label textColor = "white";
  label canFocus = false;
  label borderWidth = 0;

  label ftFontVar initFontLib;
  label ftFontVar notifyLevel XFT_NOTIFY_NONE;

  mainWindow backgroundColor = "blue";
  label resources replaceAt "backgroundColor", "blue";
  button resources replaceAt "backgroundColor", "blue";
  fileSelect resources replaceAt "backgroundColor", "blue";
  
  mainWindow initialize 255, 245;
  mainWindow inputStream eventMask = 
    EXPOSE|ENTERWINDOWNOTIFY|LEAVEWINDOWNOTIFY|BUTTONPRESS|BUTTONRELEASE|KEYPRESS|KEYRELEASE|WINDELETE|MOVENOTIFY|MOTIONNOTIFY;

  dispatcher attachTo mainWindow;
  label attachTo dispatcher, "177x60+c+35";
  button attachTo dispatcher, "110x105+c+100";
  fileSelect attachTo dispatcher, "300x400";

  mainWindow map;
  mainWindow raiseWindow;

  mainWindow openEventStream;

#ifdef FILENAME_ENTRY_BOX
  fileSelect resources replaceAt "useEntryBox", TRUE;
#endif  

  mainWindow setWMTitle "X11FileSelectDialog Demo";

  label multiLine "X11FileSelectDialog\nDemo";
  label resources replaceAt "textColor", "lightgray";
  label resources replaceAt "foregroundColor", "blue";
  label resources replaceAt "borderColor", "blue";

  button label multiLine "Open\nFile Select\nDialog";

  button label resources replaceAt "highlightForegroundColor", "gray80";

  /* The program uses the "replaceAt" method because the key/value
     entry for "backgroundColor" the X11MessageBoxPane : new method
     has alread created an entry for backgroundColor. */
  fileSelect resources replaceAt "backgroundColor", "blue";
  fileSelect resources replaceAt "foregroundColor", "blue";
  fileSelect resources replaceAt "messageColor", "white";
  fileSelect resources replaceAt "messageText",
    	       "Directory:\n";

  button draw;
  button refresh;
  label draw;
  label refresh;

  while (TRUE) {
    mainWindow inputStream queueInput;
    if (mainWindow inputStream eventPending) {
      e become mainWindow inputStream inputQueue unshift;

      mainWindow subPaneNotify e;

      switch (e eventClass value)
	{
	case EXPOSE:
	  button subPaneExpose (button, e);
	  label subPaneExpose (label, e);
	  break;
	case BUTTONRELEASE:
	  fileSelect showManaged button;
	  if (fileSelect buttonClick == FILESELECT_LBUTTON) {
	    printf ("\"%s,\" clicked.\n", fileSelect buttonText);
	    /* 
	       The button's click should be cleared, because in this
	       program, the dialog can be re-opened here (i.e., not 
	       reinstantiated with the "new" method). 

	       The, "buttonText," method is equivalent, in this clause, 
	       to, "fileSelect lbutton text."

	       The FILESELECT_LBUTTON #define and the other button
	       definitions, are in ctalkdefs.h.
	    */
	    fileSelect lbutton clearClick;
	  } else if (fileSelect entryLength > 0) {
	    printf ("%s\n", fileSelect pathEntry);
	  } else if (fileSelect nItemsSelected > 1) {
	    i = 0;
	    fileSelect selectedItems itemTextOut;
	    fileSelect selectedItemsN itemNOut;
	    itemNOut map {
	      itemN = self;
	      /* This is a convenient way to retrieve
		 the i'th item in the itemTextOut list. */
	      itemText = *(itemTextOut + i);
	      printf ("%d: %s\n", itemN, itemText);
	      ++i;
	    }
	  } else {
	    printf ("%d: %s\n",
		    fileSelect selectedItemN,
		    fileSelect selectedItemText);
	  }
	  fileSelect rbutton clearClick;
	  break;
	case WINDELETE:
 	  mainWindow deleteAndClose;
	  exit (0);
	  break;
	}
    } else {
      usleep (1000);
    }
  }

}

Retrieving Selection Information

The type and number of entries that a X11FileSelectDialog can return can differ depending on whether a user selects a single directory entry, multiple directory entries, or enters a new file name.

When returning directory entries, the class returns each entry as a concatenation of the current directory plus the entry name; for example, if the current directory is ‘/home/bill’ and the user has selected the file ‘message.txt’, the dialog returns the String, ‘/home/bill/message.txt’.

In addition, the dialog can provide information about whether the user has clicked the left or right buttons, which are labeled, by default, ‘Cancel’ and Ok.

Generally, when a program displays the file select dialog, the program waits until the dialog box returns, that is, when the user presses either the ‘Cancel’ or Ok buttons. On most desktops, the user can also close the dialog using the desktop’s close window menu item. In this case, the dialog can return a value indicating that the dialog has returned without the user making a selection or cancelling the selection. These values and the method to retrieve them are described below.

Here is the section of code from the example above (without the comments) that retrieves and displays the user’s selection or selections. The file select dialog is defined in the X11FileSelectDialog object, fileSelect.


if (fileSelect buttonClick == FILESELECT_LBUTTON) {
  printf ("\"%s,\" clicked.\n", fileSelect buttonText);
  fileSelect lbutton clearClick;
} else if (fileSelect entryLength > 0) {
  printf ("%s\n", fileSelect pathEntry);
} else if (fileSelect nItemsSelected > 1) {
  i = 0;
  fileSelect selectedItems itemTextOut;
  fileSelect selectedItemsN itemNOut;
  itemNOut map {
    itemN = self;
    itemText = *(itemTextOut + i);
    printf ("%d: %s\n", itemN, itemText);
    ++i;
  }
} else {
  printf ("%d: %s\n",
          fileSelect selectedItemN,
          fileSelect selectedItemText);
}
fileSelect rbutton clearClick;

First, the application checks whether the user clicked the ‘Cancel’ button with the expresion:


if (fileSelect buttonClick == FILESELECT_LBUTTON) 

In this class, buttonClick is a convenience method that checks each of the dialog’s buttons. It can return the following values.


FILESELECT_LBUTTON
FILESELECT_RBUTTON
FILESELECT_BUTTON_NONE

These values are defined in the ctalkdefs.h include file, which programs can include with a statement like this:


#include <ctalkdefs.h>

near the start of the file.

Assuming that the user has pressed the ‘Ok’ button, the program next checks if the user has entered a new file name, with the statement:


} else if (fileSelect entryLength > 0) {

The entryLength method returns ‘-1’ if the dialog doesn’t display an entry box, ‘0’ if no text is entered, or the length of the text. If the user has entered text, then the program can retrieve it with the pathEntry method, which retrieves and prints the entry with this line.


printf ("%s\n", fileSelect pathEntry);


Once again, the dialog returns a String that is the concatenation of the current directory and the filename that the user entered. To retrieve the entry box’s text alone, programs can use the textEntry method instead of pathEntry.

Finally, the program checks how many items the user selected from the directory entries. The program does this with the nItemsSelected method.


} else if (fileSelect nItemsSelected > 1) {

If nItemsSelected returns an Integer greater than one, the program can retrieve all of the entries as a list, with the selectedItems method, and their position in the list with the selectedItemsN method.


fileSelect selectedItems itemTextOut;
fileSelect selectedItemsN itemNOut;

These methods take one argument, a List object, here itemTextOut and itemNOut, to which the method adds either the list item text or ordinal position of each selected directory entry.

In the case where the user has selected only a single directory entry (i.e., nItemsSelected returns ‘1’), the program can use the selectedItemN and selectedItemText methods, as in this statement, which prints the information about a single entry.


printf ("%d: %s\n",
        fileSelect selectedItemN,
        fileSelect selectedItemText);

Resources

Further information about modifying resource entries is given in the resources instance variable section. See PaneResources.

listROPx
listRWPx

The distance in pixels between the bottom of the file list and the bottom of the window, in pixels. The values determine, respectively whether to display the dialog’s buttons directly beneath the list, or to display a text entry box between the file list and the buttons.

pad

The amount of space in pixels between the dialog window’s edges and the component subpane’s boundaries. The default is ‘0’.

useEntryBox

A boolean value that determines whether the dialog box displays a text entry box. The default is ‘false’.

Instance Variables

In addition to the instance variables defined in superclasses (like lbutton, rbutton, and label, which are defined in X11YesNoBoxPane class; entryPane, defined in X11TextEntryBox; and listPane, which is defined in X11ListBox class), the X11FileSelectDialog class defines the following instance variables.

dirPattern

A String that contains the file glob of the directory that the dialog displays initially. The default is . (i.e., the current directory).

dotFiles

If true, include hidden files (i.e., files whose names begin with ‘.’) in the directory listing.

targetPath

A String that contains the fully qualified path of the directory that is being displayed. The dialog object fills in the value whenever it compiles a new list of directory contents.

useEntryBox

A Boolean that determines whether the dialog should display an entry box or check for text entry operations. The default is ‘false’.

waitCursor

A X11Cursor item that defines the wait mouse pointer that the dialog can display when building directory lists.

Instance Methods

buttonClick (void)

Returns an Integer with one of the following values.


FILESELECT_LBUTTON
FILESELECT_RBUTTON
FILESELECT_BUTTON_NONE

These values are defined in ctalkdefs.h. Programs can include the statement:


#include <ctalkdefs.h>

near the start of the file.

buttonText (void)

Returns a String that contains the text of the button that the user has clicked, or NULL if no button has been clicked.

To reset the button, programs should use the clearClick method, which is defined in X11ButtonPane class; for example:


myDialog lbutton clearClick;

... or ...

myDialog rbutton clearClick;

chDir (ItemBox item)

Changes the working directory of the dialog (and the program that opens it) to the directory whose name is contained in item. If item contains ‘..’, the the new directory is the parent directory of the current directory.

draw (void)

Redraws the dialog’s label, list and button subpanes. Redrawing of the text entry pane, if it is displayed, is handled separately.

entryLength (void)

Returns the length of the text entered in the dialog’s entry box, or ‘-1’ if the dialog does not display a text entry box.

getExpandedDir (String dirPattern)

Retrieves the contents of the directory named in dirPattern, which the method qualifies and stores in the targetPath instance variable.

The method then constructs a sorted list of the directory’s contents, which it stores in listPane's items instance variable. In order to use these items directly, programs could use an expression similar to this one.


myFileDialog listPane items

initWidgets (void)

Creates the subpane objects and adds them to the dialog’s window when the dialog is first opened. Sets the initialized instance variable (defined in X11YesNoBoxPane) to ‘true’.

makeItem (String itemText)

Creates an ItemBox object (which is defined in X11ListPane class), with the text of the object, the item’s boundaries and position in pixels.

new (String paneName)

The X11FileSelectDialog constructor. The argument, a String, is the name of the new dialog object.

Note: The X11FileSelectDialog class adds the fileMode instance variable to ItemBox class, so it’s necessary to make sure that the ItemBox class, which is defined in X11ListPane. In X11FileSelectDialog class, the X11ListPane class should be loaded automatically when the listPane instance variable is created.

onExpose (Object subPane, InputEvent event)
onListClick (Object subPane, InputEvent event)
onResize (Object subPane, InputEvent event)
subPaneButton (Object subPane, InputEvent event)

The X11FileSelectDialog's event handlers. If the dialog handles events similarly to the operations in superclasses, then Ctalk uses event handlers defined in X11FileSelectDialog's superclasses.

nItemsSelected (void)

Returns an Integer with the number of list entries that are currently selected.

pathEntry (void)

If the dialog displays a text entry box, and the user has entered the name of a new file in it, returns the fully qualified path of the entry (i.e., the current directory path plus the name of text entry). If the dialog does not display an entry box, or the entry box contains no text, the method returns an empty string.

refresh (void)

Updates the dialog window with the contents of the dialog’s subpanes.

selectedItems (List selecteItemsOut)

Stores the text of each item that the user has selected in selectedItemsOut.

selectedItemN (void)

If the user has selected a single directory entry, returns the ordinal position of the item (counting from 0) as an Integer.

selectedItemsN (List selecteItemsOut)

Stores the ordinal position of enach item, counting from zero (‘0’) that the user has selected in selectedItemsOut. Each of the elements in in selectedItemsOut is an Integer object.

selectedItemText (void)

If the user has selected a single directory entry, returns the text of that item as a String.

show (void)

The main dialog box loop. This creates the dialog’s subpanes and attaches them to the main dialog window. If the dialog has already been popped up, maps the dialog on the display. Contains the event loop. When the dialog is closed, the method returns to the calling program.

textEntry (void)

If the dialog displays a text entry box, and the user has entered a file name in it, returns a String that contains the text of the entry. If the dialog does not display an entry box, or if the entry box does not contain any text, returns and empty string.

updateLabel (String qualPath)

Updates the dialog’s label with the name of the directory that is being displayed in qualPath.


Next: , Previous: , Up: Classes   [Index]