VERSION 2.00
Begin Form dde_client_form 
   Caption         =   "Test DDE Client for Neiki - v0.2"
   ClientHeight    =   2145
   ClientLeft      =   1095
   ClientTop       =   1485
   ClientWidth     =   9420
   Height          =   2550
   Left            =   1035
   LinkTopic       =   "Form1"
   ScaleHeight     =   2145
   ScaleWidth      =   9420
   Top             =   1140
   Width           =   9540
   Begin CommandButton Command1 
      Caption         =   "desc!"
      Height          =   465
      Left            =   120
      TabIndex        =   5
      Top             =   1530
      Width           =   675
   End
   Begin TextBox cmd_parameters 
      FontBold        =   -1  'True
      FontItalic      =   0   'False
      FontName        =   "MS Sans Serif"
      FontSize        =   9.75
      FontStrikethru  =   0   'False
      FontUnderline   =   0   'False
      Height          =   375
      Left            =   1260
      TabIndex        =   0
      Text            =   "10 mar 1995,20:59:00,5,73.35,45.33"
      Top             =   180
      Width           =   6495
   End
   Begin TextBox dde_msg_ctl 
      Height          =   1185
      Left            =   960
      LinkTimeout     =   100
      MultiLine       =   -1  'True
      TabIndex        =   1
      Text            =   "(idle)"
      Top             =   810
      Width           =   8355
   End
   Begin CommandButton do_query_cmd 
      Caption         =   "Compute"
      FontBold        =   -1  'True
      FontItalic      =   0   'False
      FontName        =   "MS Sans Serif"
      FontSize        =   9.75
      FontStrikethru  =   0   'False
      FontUnderline   =   0   'False
      Height          =   555
      Left            =   7980
      TabIndex        =   3
      Top             =   90
      Width           =   1275
   End
   Begin Label Label2 
      Alignment       =   1  'Right Justify
      Caption         =   "Results:"
      Height          =   285
      Left            =   120
      TabIndex        =   4
      Top             =   810
      Width           =   795
   End
   Begin Label Label1 
      Alignment       =   1  'Right Justify
      Caption         =   "Data input:"
      Height          =   285
      Left            =   60
      TabIndex        =   2
      Top             =   270
      Width           =   1035
   End
End
Option Explicit
'***
'*** Test DDE Client (v0.1) for Neiki Astro Tool Kit
'***
'*** (c) Copyright 1995 by Falgor, Archimede & Neiki
'***
'*** Permission is hereby granted to modify and use for
'*** personal purposes.
'***
'*** Software developpers should contact the author
'*** and make arrangements before using this source
'*** code in their software product(s) as the DDE link
'*** protocol to Neiki/Archimede is more complex
'*** than the simple example shown here.
'***
'*** ----------------------------------------------------
'*** Version history:
'*** 1995march11: v0.1 - as an example
'*** 1995march12: v0.2 - with "posSequenceDescription" command
'***

'*** Form Global (private) variables:

Dim s_appl_prefix As String 'Prefix of application

Sub Command1_Click ()
    Dim res As Integer

    '*** Send the command to the DDE Server:
    '***
    res = ddef_send_cmd(dde_msg_ctl, "posSequenceDescription", "none")

    '*** always res = 0 for now! ***
    
    If res <> 0 Then
	MsgBox "Send command failed - return code =" & Str$(res)
	Exit Sub
    End If

    '*** Fetch the results from the DDE Server:
    '***
    dde_msg_ctl.LinkRequest

End Sub

Function ddef_link_to_dde_server (txt_ctl As TextBox) As Integer
    Dim ctr As Integer

    ctr = 0
    On Error GoTo dde_server_missing
restart_link:
    ctr = ctr + 1
    txt_ctl.LinkMode = 0            'NONE - Begin init of ctl

    txt_ctl.LinkTopic = "NeikiDDEServer|CalcChart"
    txt_ctl.LinkItem = "results"
    
    txt_ctl.LinkMode = 2            'LINK_MANUAL - make the link
    
    ddef_link_to_dde_server = 0     'OK
    txt_ctl.LinkRequest             'Connect and fetch any data
    Exit Function

dde_server_missing:
    ddef_link_to_dde_server = -1    'Error occurred
    If Err = 282 Then
	If ctr > 2 Then             'Resume only once!
	    Exit Function
	End If
	MsgBox "Please start: 'Neiki Astro Tool Kit' application and Enable the DDE Server mode"
	Resume restart_link
    ElseIf Err = 285 Then
	MsgBox "LinkItem '" + txt_ctl.LinkItem + "' is wrong."
    Else
	MsgBox "err=" + Str$(Err)
	Error Err
    End If

End Function

Function ddef_prep_command (cmd As String, args As String) As String
    'Build a DDE command to be sent
    'NOTE: args must already be the concatenation of all
    '      arguments.
    ' Example:
    '     string content would be:
    '        "10 mar 1995,21:55,5,73.35,45.33"
    '
    ddef_prep_command = s_appl_prefix + ":" + cmd + "(" + args + ")"

End Function

Function ddef_send_cmd (ctl As TextBox, cmdx As String, args As String) As Integer
    Dim full_cmd As String

    Screen.MousePointer = 11        'Hourglass cursor

    full_cmd = ddef_prep_command(cmdx, args)

    ctl.LinkTimeout = 100   '*** Timeout (in tenths of second)

    ctl.LinkExecute full_cmd

    ddef_send_cmd = 0
    Screen.MousePointer = 0     'Arrow cursor
End Function

Sub do_query_cmd_Click ()
    Dim inp_param_str As String
    Dim res As Integer

    inp_param_str = Trim$(cmd_parameters.Text)
    If inp_param_str = "" Then
	MsgBox "Parameters empty!"
	Exit Sub
    End If

    '*** Send the command to the DDE Server:
    '***
    res = ddef_send_cmd(dde_msg_ctl, "CalcChart", inp_param_str)
    '***
    '*** execution will continue here after the
    '*** DDE server acknowledges the command
    '*** or after the Timeout period expires
    '*** (see ctl.LinkTimeout property).

    '*** always res = 0 for now! ***
    
    If res <> 0 Then
	MsgBox "Send command failed - return code =" & Str$(res)
	Exit Sub
    End If

    '*** Fetch the results from the DDE Server:
    '***
    dde_msg_ctl.LinkRequest
End Sub

Sub Form_Load ()
    s_appl_prefix = Left$(app.EXEName, 8)

    If ddef_link_to_dde_server(dde_msg_ctl) <> 0 Then
	MsgBox "Link to Neiki's DDE Server Failed"
	End
    End If
End Sub

Sub Form_QueryUnload (Cancel As Integer, UnloadMode As Integer)
    dde_msg_ctl.LinkMode = 0
End Sub

