Index: tkMacOSXDialog.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/macosx/tkMacOSXDialog.c,v
retrieving revision 1.4.2.2
diff -p -p -r1.4.2.2 tkMacOSXDialog.c
*** tkMacOSXDialog.c    16 Feb 2004 00:42:34 -0000      1.4.2.2
--- tkMacOSXDialog.c    13 Mar 2004 00:04:48 -0000
***************
*** 13,18 ****
--- 13,19 ----
   */
  #include <Carbon/Carbon.h>
  
+ #include "tcl.h"
  #include "tkPort.h"
  #include "tkInt.h"
  #include "tkMacOSXInt.h"
*************** static void             InitFileDialogs(
*** 82,93 ****
  static int              NavServicesGetFile(Tcl_Interp *interp, OpenFileData *ofd,
                              AEDesc *initialDescPtr,
                              unsigned char *initialFile, AEDescList *selectDescPtr,
!                             CFStringRef title, CFStringRef message, int multiple, int isOpen);
! static int              HandleInitialDirectory (Tcl_Interp *interp,
!                                                 char *initialFile, char *initialDir,
!                                                 FSRef *dirRef,
!                                                 AEDescList *selectDescPtr,
!                                                 AEDesc *dirDescPtr);                            
  
  /*
   * Have we initialized the file dialog subsystem
--- 83,97 ----
  static int              NavServicesGetFile(Tcl_Interp *interp, OpenFileData *ofd,
                              AEDesc *initialDescPtr,
                              unsigned char *initialFile, AEDescList *selectDescPtr,
!                             CFStringRef title, CFStringRef message, 
!                             int multiple, int isOpen);
! static int              HandleInitialDirectory (Tcl_Interp *interp, 
!                             Tcl_Obj *initialSelection,
!                             int multiple,
!                             char *initialDir,
!                             FSRef *dirRef,
!                             AEDescList *selectDescPtr,
!                             AEDesc *dirDescPtr);                            
  
  /*
   * Have we initialized the file dialog subsystem
*************** Tk_GetOpenFileObjCmd(
*** 271,277 ****
      FSRef dirRef;
      AEDesc *initialPtr = NULL;
      AEDescList selectDesc = {typeNull, NULL};
!     char *initialFile = NULL, *initialDir = NULL;
      static CONST char *openOptionStrings[] = {
              "-defaultextension", "-filetypes", 
              "-initialdir", "-initialfile", 
--- 275,282 ----
      FSRef dirRef;
      AEDesc *initialPtr = NULL;
      AEDescList selectDesc = {typeNull, NULL};
!     char *initialDir = NULL;
!     Tcl_Obj *initialSelection = NULL;
      static CONST char *openOptionStrings[] = {
              "-defaultextension", "-filetypes", 
              "-initialdir", "-initialfile", 
*************** Tk_GetOpenFileObjCmd(
*** 334,340 ****
                  initialDir = Tcl_GetStringFromObj(objv[i + 1], NULL);
                  break;
              case OPEN_INITFILE:
!                 initialFile = Tcl_GetStringFromObj(objv[i + 1], NULL);
                  break;
              case OPEN_MESSAGE:
                  choice = Tcl_GetStringFromObj(objv[i + 1], &choiceLen);
--- 339,345 ----
                  initialDir = Tcl_GetStringFromObj(objv[i + 1], NULL);
                  break;
              case OPEN_INITFILE:
!                 initialSelection = objv[i + 1];
                  break;
              case OPEN_MESSAGE:
                  choice = Tcl_GetStringFromObj(objv[i + 1], &choiceLen);
*************** Tk_GetOpenFileObjCmd(
*** 364,371 ****
          }
      }
  
!     if (HandleInitialDirectory(interp, initialFile, initialDir, &dirRef,
!                                &selectDesc, &initialDesc) != TCL_OK) {
          result = TCL_ERROR;
          goto end;
      }
--- 369,377 ----
          }
      }
  
!     if (HandleInitialDirectory(interp, initialSelection, multiple, 
!             initialDir, &dirRef,
!             &selectDesc, &initialDesc) != TCL_OK) {
          result = TCL_ERROR;
          goto end;
      }
*************** Tk_GetSaveFileObjCmd(
*** 466,472 ****
                  break;
              case SAVE_INITDIR:
                  choice = Tcl_GetStringFromObj(objv[i + 1], NULL);
!                 if (HandleInitialDirectory(interp, NULL, choice, &dirRef, 
                          NULL, &initialDesc) != TCL_OK) {
                      result = TCL_ERROR;
                      goto end;
--- 472,478 ----
                  break;
              case SAVE_INITDIR:
                  choice = Tcl_GetStringFromObj(objv[i + 1], NULL);
!                 if (HandleInitialDirectory(interp, NULL, 0, choice, &dirRef, 
                          NULL, &initialDesc) != TCL_OK) {
                      result = TCL_ERROR;
                      goto end;
*************** Tk_ChooseDirectoryObjCmd(clientData, int
*** 599,605 ****
          switch (index) {
              case CHOOSE_INITDIR:
                  choice = Tcl_GetStringFromObj(objv[i + 1], NULL);
!                 if (HandleInitialDirectory(interp, NULL, choice, &dirRef,  
                          NULL, &initialDesc) != TCL_OK) {
                      result = TCL_ERROR;
                      goto end;
--- 605,611 ----
          switch (index) {
              case CHOOSE_INITDIR:
                  choice = Tcl_GetStringFromObj(objv[i + 1], NULL);
!                 if (HandleInitialDirectory(interp, NULL, 0, choice, &dirRef,  
                          NULL, &initialDesc) != TCL_OK) {
                      result = TCL_ERROR;
                      goto end;
*************** Tk_ChooseDirectoryObjCmd(clientData, int
*** 648,663 ****
      return result;
  }
  
! int
  HandleInitialDirectory (
      Tcl_Interp *interp,
!     char *initialFile,
      char *initialDir,
      FSRef *dirRef, 
      AEDescList *selectDescPtr,
      AEDesc *dirDescPtr)
  {
      Tcl_DString ds;
      OSErr err;
      Boolean isDirectory;
      char *dirName = NULL;
--- 654,671 ----
      return result;
  }
  
! static int
  HandleInitialDirectory (
      Tcl_Interp *interp,
!     Tcl_Obj *initialSelection,
!     int multiple,
      char *initialDir,
      FSRef *dirRef, 
      AEDescList *selectDescPtr,
      AEDesc *dirDescPtr)
  {
      Tcl_DString ds;
+     int dsLen;
      OSErr err;
      Boolean isDirectory;
      char *dirName = NULL;
*************** HandleInitialDirectory (
*** 665,670 ****
--- 673,679 ----
  
      if (initialDir != NULL) {
          dirName = Tcl_TranslateFileName(interp, initialDir, &ds);
+         dsLen = Tcl_DStringLength(&ds);
          if (dirName == NULL) {
              return TCL_ERROR;
          }
*************** HandleInitialDirectory (
*** 688,719 ****
          AECreateDesc(typeFSRef, dirRef, sizeof(*dirRef), dirDescPtr);
      }
  
!     if (initialFile != NULL && selectDescPtr != NULL) {
          FSRef fileRef;
          AEDesc fileDesc;
          char *namePtr;
! 
!         if (initialDir != NULL) {
!             Tcl_DStringAppend(&ds, "/", 1);
!             Tcl_DStringAppend(&ds, initialFile, -1);
!             namePtr = Tcl_DStringValue(&ds);
          } else {
!             namePtr = initialFile;
          }
! 
          AECreateList(NULL, 0, false, selectDescPtr);
  
!         err = FSPathMakeRef(namePtr, &fileRef, &isDirectory);
!         if (err != noErr) {
!             Tcl_AppendResult(interp, "bad initialfile \"", initialFile,
!                     "\" file does not exist.", NULL);
!             return TCL_ERROR;
          }
-         AECreateDesc(typeFSRef, &fileRef, sizeof(fileRef), &fileDesc);
-         AEPutDesc(selectDescPtr, 1, &fileDesc);
-         AEDisposeDesc(&fileDesc);
      }
- 
  end:
      if (dirName != NULL) {
          Tcl_DStringFree(&ds);
--- 697,752 ----
          AECreateDesc(typeFSRef, dirRef, sizeof(*dirRef), dirDescPtr);
      }
  
!     if (initialSelection != NULL && selectDescPtr != NULL) {
          FSRef fileRef;
          AEDesc fileDesc;
          char *namePtr;
!         Tcl_Obj **selArray;
!         Tcl_Obj *oneSel[1];
!         int selCount, i, result;
!         
!         if (multiple) {
!             result = Tcl_ListObjGetElements (interp, initialSelection, 
!                     &selCount, &selArray);
!             if (result != TCL_OK) {
!                 Tcl_AppendResult (interp, "initial file list is not a proper list.",
!                     NULL);
!                 return TCL_ERROR;
!             }
          } else {
!             selArray = oneSel;
!             oneSel[0] = initialSelection;
!             selCount = 1;
          }
!         
          AECreateList(NULL, 0, false, selectDescPtr);
  
!         for (i = 0; i < selCount; i++) {
!             char *initialFile;
!             int len;
!             
!             initialFile = Tcl_GetStringFromObj (selArray[i], &len);
!             
!             if (initialDir != NULL) {
!                 Tcl_DStringSetLength (&ds, dsLen);
!                 Tcl_DStringAppend(&ds, "/", 1);
!                 Tcl_DStringAppend(&ds, initialFile, len);
!                 namePtr = Tcl_DStringValue(&ds);
!             } else {
!                 namePtr = initialFile;
!             }
! 
!             err = FSPathMakeRef(namePtr, &fileRef, &isDirectory);
!             if (err != noErr) {
!                 Tcl_AppendResult(interp, "bad initialfile \"", initialFile,
!                         "\" file does not exist.", NULL);
!                 return TCL_ERROR;
!             }
!             AECreateDesc(typeFSRef, &fileRef, sizeof(fileRef), &fileDesc);
!             AEPutDesc(selectDescPtr, i + 1, &fileDesc);
!             AEDisposeDesc(&fileDesc);
          }
      }
  end:
      if (dirName != NULL) {
          Tcl_DStringFree(&ds);
