'------------------------------------------------------------------------------
'Copyright (c) 2004 James W. Grenning
'------------------------------------------------------------------------------
Sub NewTestableCppClass()

	on error resume next

	classname = InputBox(EnterClassNamePrompt, "Testable Class Generator" ) '][, default][, xpos][, ypos][, helpfile, context])
	if classname = "" then exit sub

	fileRoot = RootFileName(classname)
	Call MakeProjectFile(classname, TemplatesDirectory() + "ClassName.cpp", fileRoot + ".cpp")
	Call MakeProjectFile(classname, TemplatesDirectory() + "ClassName.h", fileRoot + ".h")
	Call MakeProjectFile(classname, TemplatesDirectory() + "ClassNameTest.cpp", fileRoot + "Test.cpp")

End Sub

Sub NewHeaderOnlyClass()

	on error resume next

	classname = InputBox(EnterClassNamePrompt, "Header File Class Generator" ) '][, default][, xpos][, ypos][, helpfile, context])
	if classname = "" then exit sub

	fileRoot = RootFileName(classname)
	Call MakeProjectFile(classname, TemplatesDirectory() + "ClassName.h", fileRoot + ".h")

End Sub

Sub Test

	'classname = InputBox(EnterClassNamePrompt, "Testable Class Generator" ) '][, default][, xpos][, ypos][, helpfile, context])

	''env = Environ("CPPTEST_LIB")

	''Call ShowItem("ENV=", env)


'     TheFile = "C:\AUTOEXEC.BAT"
'         Results = Dir$(TheFile)
'
'         If Results  = "" Then
'            MsgBox "File Doesn't Exist!"
'         Else
'            MsgBox "File does Exist!"
'         End If
'      End Sub
'
'  templates = TemplatesDirectory
'
'  Call ShowItem("Templates", templates)

end sub

Private Function EnterClassNamePrompt()

	prompt  = "Enter class name to add to Project '" + ActiveProject.Name + "' or press Cancel"
	warning = " NOTE: a duplicate file name will cause Visual Studio to crash"    
	EnterClassNamePrompt = prompt + warning

end Function


Private Function TemplatesDirectory()

  TemplatesDirectory = "D:\tools\fitnesse20040408\cpp\CppSourceTemplates\"

end Function

Private Function RootFileName(classname)

	fullproject = ActiveProject.fullname
	project = ActiveProject.name
    projectpath = Left(fullproject, len(fullproject) - len(project) - 4)
	RootFileName = projectpath + classname
	
end function

Private Sub ShowItem(label, item)
	MsgBox label + ": " + item
end sub

Private Sub MakeProjectFile(classname, template, target)
	on error resume next

	Documents.Open template, "Text"
	ActiveDocument.Selection.SelectAll
	ActiveDocument.Selection.Copy
	ActiveWindow.Close dsSaveChangesPrompt
	Documents.Add "Text"
	ActiveDocument.Language = "C/C++"
	ActiveDocument.Selection.Paste

	defineClassname = "D_" + UCase(classname) + "_H"
	ActiveDocument.ReplaceText "D_ClassName_H", defineclassname

	instancename = LCase(classname)
	ActiveDocument.ReplaceText "aClassName", instancename

	ActiveDocument.ReplaceText "ClassName", classname

	ActiveDocument.Save target
	ActiveProject.AddFile target
	ActiveDocument.Save

End Sub

Private Sub CheckFileExists 'i can't get this to work

	classname = InputBox("Enter class name (null to cancel)", "New Class Generator") '][, default][, xpos][, ypos][, helpfile, context])
	if classname = "" then exit sub

	fullproject = ActiveProject.fullname
	project = ActiveProject.name
    projectpath = Left(fullproject, len(fullproject) - len(project) - 4)
	fileRoot = projectpath + classname

    Set fso = CreateObject("Scripting.FileSystemObject")
    If (fso.FileExists(target)) Then
       msg = target & " exists."
	   MsgBox msg
	   exit sub
    End If

end sub



