/* C-example file of how to load Spectrum Viewer and control it with DDE. */

/* This file was compiled with National Instruments LabWindows and the Windows SDK. */
/* There are probably some commands in here that are not supported by your compiler. */
/* I left them in here so you have a good starting point to look for commands to use */
/* with your own compiler. */


#define SV_DDE_NAME "SV Server" /* name under which SV server is registered */
#define SV_DDE_TOPIC "SV Client 1" /* topic of the conversation */

/* Commands supported by the server: */
#define SV_FILE_OPEN "FileOpen"
#define SV_FILE_CLOSE "FileClose"

/* FileOpen takes one argument, the file name. It tells SV to open the specific file. */
/* FileClose takes one argument as well, also the file name. FileClose tells SV to */
/* close every data set that originated in the specified data file (there can be */
/* multiple data sets in one file). You would use this if you update a file on disk, */
/* and then want SV to display the updated file, not the old version. You then first */
/* send a FileClose, and then a FileOpen. */


static unsigned DDEconvID = -1; // convID - the client's conversation handle for the DDE connection

int SendtoSV(char *FileName, int DeleteOld, char *OldFileName)
{
/* This function sends a new file to Spectrum Viewer, and deletes an old data set */

    if (ConnectToDDEServer (&DDEconvID, SV_DDE_NAME, SV_DDE_TOPIC, ClientDDECallback, 0) == OK) {
    /* The SV program was already loaded, and the DDE server registered. */
    /* Connection went OK. Send the file with DDE */

        if ((DeleteOld) && (OldFileName))
            ClientDDEWrite (DDEconvID, SV_FILE_CLOSE, CF_TEXT, OldFileName, strlen(OldFileName) + 1, 4000);

        ClientDDEWrite (DDEconvID, SV_FILE_OPEN, CF_TEXT, FileName, strlen(FileName) + 1, 2000);

        DisconnectFromDDEServer (DDEconvID);

    } /* if (Connnect == OK) */

    else {
    /* The DDE server wasn't active, so the SV program is probably not loaded. Launch the program */

        LoadSpecViewer(FileName);
    }
}

int LoadSpecViewer(char *FileToOpen)
{
    int Status;
    static int SpecViewHdl = 0;
    char CommandString[400];

    strcpy(CommandString, "SpecView.exe ");

    if (FileToOpen) {
    /* Make sure there are no spaces in the file name path */

        char ShortPath[MAX_PATHNAME_LEN];
        GetShortPathName(FileToOpen, ShortPath, MAX_PATHNAME_LEN); /* this is a windows SDK command */
        strcat(CommandString, ShortPath); 
        strcat(CommandString, " "); 
    }

    /* command line arguments: */

    strcat(CommandString, "-L 0 "); // no load last session

    // initial open file folder
    if (strlen(InputDir)) {
        strcat(CommandString, "-I ");
        strcat(CommandString, InputDir);
        strcat(CommandString, " ");
    }

    // default file spec, fill in here your favorite measure file extensions
    strcat(CommandString, "-S ");
    strcat(CommandString, " *.svf;*.txt; ");

    // default file extensions
    strcat(CommandString, "-E *.txt;*.svf ");

    Status = LaunchExecutableEx (CommandString, LE_SHOWNA, &SpecViewHdl);
    if (Status) DisplayErrorMessage("Load ERROR","Cannot load Spectrum Viewer executable",Status);
    return Status;
}


/* The callback used by the LabWindows DDE client, your compiler probably uses something completely different */
int CVICALLBACK ClientDDECallback (unsigned int handle, char *topicName, char *itemName, int xType,
                                    int dataFmt, int dataSize, void *dataPtr, void *callbackData)
{
    if (handle != DDEconvID)  // checks message validity. Only acknowledges 
        return FALSE;         // messages pertaining to one conversation (convID) 

    switch (xType) {          // determines message type 
   
     case DDE_DISCONNECT:
            DDEconvID = -1;   // kills conversation when server unregisters 
            break;

        default:
            return FALSE;
    }

    return TRUE;              // TRUE --> client processed message successfully 
}