home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 January / dppcpro0199a.iso / January / Fp98 / SDK / Wizards / Vb / wizutil.bas < prev    next >
Encoding:
BASIC Source File  |  1997-09-18  |  16.8 KB  |  612 lines

  1. Attribute VB_Name = "WIZUTIL"
  2. ' -------------------------------------------------------------------
  3. ' File: WIZUTIL.BAS
  4. ' Copyright (c) 1995, 1996 Microsoft Corporation.  All rights reserved.
  5. ' -------------------------------------------------------------------
  6.  
  7. Option Explicit
  8.  
  9. Global Const FrontPageExplorerID = "FrontPage.Explorer"
  10. Global Const FrontPageEditorID = "FrontPage.Editor"
  11. Global Const FrontPageToDoListID = "FrontPage.ToDoList"
  12.  
  13. Const FrontPageIniName = "frontpg.ini"
  14. Const FrontPageSection = "FrontPage 3.0"
  15. Const FrontPageRootKey = "FrontPageRoot"
  16. Const FrontPagePWSKey = "PWSRoot"
  17. Const FrontPageHostKey = "CheckedHostName"
  18.  
  19. ' essential dirs in FrontPage installation
  20. Global FrontPageRootDir As String
  21. Global FrontPageDataDir As String
  22. Global FrontPageTempDir As String
  23. Global FrontPageWebsDir As String
  24. Global FrontPagePagesDir As String
  25. ' extra stuff for installed Personal Web Server
  26. Global FrontPageServerRoot As String
  27. Global FrontPageServerHost As String
  28.  
  29. ' NOTE: you'll have to do extra work if
  30. ' you want to generate absolute URLs;
  31. ' you'll have to sift through the [Ports]
  32. ' section in frontpg.ini to make sure you
  33. ' have the correct port number; this isn't
  34. ' necessary for most applications
  35.  
  36. ' command-line info: path to temp file
  37. ' holding arguments
  38. Global WizardArgFile As String
  39. ' named sections within arg file
  40. Const WizardInputSection = "Input"
  41. Const WizardOutputSection = "Output"
  42. Const WizardEnvironmentSection = "Environment"
  43. ' keys for built-in arguments in Input section
  44. Const WizardDirKey = "Dir"
  45. Const WizardInfKey = "Inf"
  46. Const WizardBlockingKey = "Blocking"
  47. Const WizardEditingKey = "Editing"
  48. Const WizardPageURLKey = "PageURL"
  49. Const WizardPageFileKey = "PageFile"
  50. Const WizardPageTitleKey = "PageTitle"
  51. Const WizardDestinationKey = "Destination"
  52. ' keys for built-in arguments in Output section
  53. Const WizardExitStatusKey = "ExitStatus"
  54. Const WizardFileCountKey = "FileCount"
  55. ' built-in values for exit status key
  56. Global Const WizardStatusOK = "ok"
  57. Global Const WizardStatusCancel = "cancel"
  58. Global Const WizardStatusError = "error"
  59.  
  60. ' data fetched from wizard args file
  61. Global WizardDir As String
  62. Global WizardInf As String
  63. Global WizardBlocking As Boolean
  64. Global WizardEditing As Boolean
  65. Global WizardPageURL As String
  66. Global WizardPageFile As String
  67. Global WizardPageTitle As String
  68. Global WizardDestination As String
  69.  
  70. ' path to wizard settings file (for persistent state);
  71. ' this *must* be in the FrontPageData dir, not the
  72. ' wizard dir itself; this is so that wizard dirs can
  73. ' be shared from a read-only file system
  74. Global WizardIniFile As String
  75. ' name of section where all wizard settings are stored
  76. Const WizardSettingsSection = "Settings"
  77. Global WizardHasPreviousSettings As Boolean ' this is true if the file exists
  78.  
  79. ' list of files to be uploaded
  80. Type FileInfo
  81.     Name As String     ' name of disk file
  82.     path As String
  83.     IsNew As Integer   ' is file being generated by wiz or not?
  84.     title As String    ' brief description of file
  85. End Type
  86. Global Files() As FileInfo
  87. Global nfiles As Integer
  88.  
  89. ' list of web meta-info variables
  90. Type MetaVar
  91.     Name As String
  92.     value As String
  93. End Type
  94. Global MetaVars() As MetaVar
  95. Dim nMetaVars As Integer
  96.  
  97. Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
  98. Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
  99. Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
  100. Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
  101. Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
  102. Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
  103. Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
  104. Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
  105. Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
  106.  
  107. ' declare constants for use with GetWindow
  108.  
  109. Const GW_HWNDFIRST = 0
  110. Const GW_HWNDLAST = 1
  111. Const GW_HWNDNEXT = 2
  112. Const GW_HWNDPREV = 3
  113.  
  114. ' declare constants for changing window position and Z-order
  115.  
  116. Global Const SWP_NOMOVE = 2
  117. Global Const SWP_NOSIZE = 1
  118. Global Const HWND_TOPMOST = -1
  119. Global Const HWND_NOTOPMOST = -2
  120.  
  121.  
  122. Sub CenterFormOnScreen(f As Form)
  123.  
  124.     Dim x As Integer
  125.     Dim y As Integer
  126.  
  127.     x = Screen.Width / 2 - f.Width / 2
  128.     y = Screen.Height / 2 - f.Height / 2
  129.  
  130.     f.Move x, y
  131.  
  132. End Sub
  133.  
  134. Public Function FindWindow(hWndBase As Long, initstr As String) As Long
  135.     
  136.     Dim hWnd As Long
  137.     Dim hWndLast As Long
  138.     Dim slen As Integer
  139.     Dim sbuf As String
  140.     
  141.     ' search for a window in the stack where the
  142.     ' initial chars in its title match 'initstr'
  143.     
  144.     hWnd = GetWindow(hWndBase, GW_HWNDFIRST)
  145.     hWndLast = GetWindow(hWndBase, GW_HWNDLAST)
  146.     Do
  147.         sbuf = String$(255, 0)
  148.         slen = GetWindowText(hWnd, sbuf, 255)
  149.         sbuf = Left$(sbuf, slen)
  150.         If Left$(sbuf, Len(initstr)) = initstr Then
  151.             ' found
  152.             FindWindow = hWnd
  153.             Exit Function
  154.         End If
  155.         hWnd = GetWindow(hWnd, GW_HWNDNEXT)
  156.     Loop While hWnd <> hWndLast
  157.     
  158.     ' not found
  159.     FindWindow = 0
  160.     
  161. End Function
  162.  
  163. Public Sub SafeSendKeys(hWnd As Long, keystr As String, Optional sync)
  164.  
  165.     ' send a key stream to a given window,
  166.     ' but first make it the foreground active
  167.     ' window and lock it at the top of the
  168.     ' window stack until the stream is finished
  169.     
  170.     If IsMissing(sync) Then sync = False
  171.      
  172.     SetForegroundWindow hWnd
  173.     MakeFloatingWindow hWnd, True
  174.     SendKeys keystr, sync
  175.     MakeFloatingWindow hWnd, False
  176.  
  177. End Sub
  178. Function GetIniBool(key As String) As Boolean
  179.  
  180.     ' wizard-specific routine for reading previous settings
  181.     Dim str As String
  182.     str = GetIniString(key)
  183.     If Len(str) = 0 Then
  184.         GetIniBool = False
  185.     Else
  186.         GetIniBool = CBool(str)
  187.     End If
  188.  
  189. End Function
  190.  
  191. Function GetIniInt(key As String) As Integer
  192.  
  193.     ' wizard-specific routine for reading previous settings
  194.  
  195.     GetIniInt = CInt(GetIniString(key))
  196.  
  197. End Function
  198.  
  199. Function GetIniString(key As String) As String
  200.  
  201.     ' wizard-specific routine for reading previous settings
  202.     
  203.     GetIniString = GetIniValue(WizardIniFile, WizardSettingsSection, key)
  204.  
  205. End Function
  206.  
  207. Sub InitFiles()
  208.  
  209.     ReDim Files(50)
  210.     nfiles = 0
  211.  
  212. End Sub
  213.  
  214. Sub InitMetaVars()
  215.  
  216.     ReDim MetaNames(50)
  217.     ReDim MetaVars(50)
  218.     nMetaVars = 0
  219.  
  220. End Sub
  221.  
  222. Sub MakeFloatingForm(f As Form, yesno As Integer)
  223.  
  224.     Dim ret As Long
  225.  
  226.     If yesno Then
  227.         ret = SetWindowPos(f.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
  228.     Else
  229.         ret = SetWindowPos(f.hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
  230.     End If
  231.  
  232. End Sub
  233.  
  234. Function PackMetaInfoVars() As String
  235.  
  236.     Dim i As Integer
  237.     Dim out As String
  238.  
  239.     out = ""
  240.  
  241.     For i = 0 To nMetaVars - 1
  242.         out = out & MetaVars(i).Name & ":" & MetaVars(i).value
  243.         If (i + 1) < nMetaVars Then out = out & Chr$(10)
  244.     Next i
  245.  
  246.     PackMetaInfoVars = out
  247.  
  248. End Function
  249.  
  250.  
  251.  
  252. Sub SetIniBool(key As String, truth As Boolean)
  253.  
  254.     ' wizard-specific routine for storing current settings
  255.  
  256.     SetIniString key, CStr(truth)
  257.  
  258. End Sub
  259.  
  260. Sub SetIniInt(key As String, keyval As Integer)
  261.  
  262.     ' wizard-specific routine for storing current settings
  263.  
  264.     SetIniString key, CStr(keyval)
  265.     
  266. End Sub
  267.  
  268. Sub SetIniString(key As String, keyval As String)
  269.     
  270.     ' wizard-specific routine for storing current settings
  271.     
  272.     SetIniValue WizardIniFile, WizardSettingsSection, key, keyval
  273.     
  274. End Sub
  275.  
  276.  
  277.  
  278. Function TempFileName() As String
  279.  
  280.     ' creates a temporary file, in the FrontPage temp dir
  281.     ' if possible, and returns the absolute path to it
  282.  
  283.     Dim ret As Long
  284.     Dim filename As String
  285.     Dim dirname As String
  286.     Dim drive As Integer
  287.     Dim dloc As Integer
  288.     
  289.     If Len(FrontPageTempDir) > 0 Then
  290.         dirname = FrontPageTempDir
  291.     Else
  292.         dirname = Space$(256)
  293.         ret = GetTempPath(256, dirname)
  294.         dirname = Left$(dirname, ret)
  295.     End If
  296.  
  297.     filename = Space$(256)
  298.     ret = GetTempFileName(dirname, "wiz", 0, filename)
  299.     If ret = 0 Then
  300.         TempFileName = ""
  301.     Else
  302.         dloc = InStr(LCase$(filename), ".tmp")
  303.         If dloc > 0 Then
  304.             filename = Left$(filename, dloc + 3) ' get rid of trailing NULL
  305.         End If
  306.         TempFileName = filename
  307.     End If
  308.  
  309. End Function
  310.  
  311. Sub AddFile(filename As String, status As Integer, descr As String)
  312.  
  313.     Dim asize As Integer
  314.  
  315.     If nfiles >= UBound(Files) Then
  316.         If UBound(Files) < 50 Then
  317.             asize = 100
  318.         Else
  319.             asize = UBound(Files) * 2
  320.         End If
  321.         ReDim Files(asize)
  322.     End If
  323.  
  324.     Files(nfiles).Name = filename
  325.     Files(nfiles).IsNew = status
  326.     If descr = "" Then
  327.         Files(nfiles).title = filename
  328.     Else
  329.         Files(nfiles).title = descr
  330.     End If
  331.     Files(nfiles).path = ""
  332.     nfiles = nfiles + 1
  333.  
  334. End Sub
  335.  
  336. Public Function StringFromFile(filename As String) As String
  337.  
  338.     Dim f As Integer  ' file number
  339.     Dim out As String ' output
  340.     Dim ln As String  ' line of input
  341.     Dim nl As String  ' end of line chars
  342.  
  343.     ' TODO: watch out for 32K string limit
  344.  
  345.     On Error GoTo StringFromFileError
  346.  
  347.     nl = Chr$(13) & Chr$(10)  ' newline
  348.     out = ""  ' initialize string
  349.  
  350.     f = FreeFile
  351.  
  352.     Open filename For Input As f
  353.  
  354.     Do While Not EOF(f)
  355.         Line Input #f, ln
  356.         out = out & ln & nl
  357.     Loop
  358.  
  359.     Close f
  360.  
  361.     StringFromFile = out
  362.  
  363.     Exit Function
  364.  
  365. StringFromFileError:
  366.     ' file not found
  367.     StringFromFile = ""
  368.     Exit Function
  369.  
  370. End Function
  371.  
  372. Sub AddMetaVar(varname As String, varvalue As String)
  373.  
  374.     Dim asize As Integer
  375.  
  376.     If nMetaVars >= UBound(MetaVars) Then
  377.         If UBound(MetaVars) < 50 Then
  378.             asize = 100
  379.         Else
  380.             asize = UBound(MetaVars) * 2
  381.         End If
  382.         ReDim MetaVars(asize)
  383.     End If
  384.  
  385.     MetaVars(nMetaVars).Name = varname
  386.     MetaVars(nMetaVars).value = varvalue
  387.     nMetaVars = nMetaVars + 1
  388.  
  389. End Sub
  390.  
  391.  
  392.  
  393.  
  394. Public Sub GetFrontPageInfo()
  395.  
  396.     FrontPageRootDir = GetIniValue(FrontPageIniName, FrontPageSection, FrontPageRootKey)
  397.     FrontPageDataDir = FrontPageRootDir & "\data"
  398.     FrontPageTempDir = FrontPageRootDir & "\temp"
  399.     FrontPageWebsDir = FrontPageRootDir & "\webs"
  400.     FrontPagePagesDir = FrontPageRootDir & "\pages"
  401.     
  402.     FrontPageServerRoot = GetIniValue(FrontPageIniName, FrontPageSection, FrontPagePWSKey)
  403.     FrontPageServerHost = GetIniValue(FrontPageIniName, FrontPageSection, FrontPageHostKey)
  404.     
  405. End Sub
  406.  
  407. Public Function GetIniValue(inifile As String, section As String, key As String) As String
  408.  
  409.     ' generic routine for reading INI values from any file
  410.  
  411.     Dim strval As String
  412.     Dim valid As Long
  413.     Dim size As Integer
  414.         
  415.     strval = Space$(2048)
  416.     size = Len(strval)
  417.  
  418.     valid = GetPrivateProfileString(section, key, "*NO-VALUE*", strval, size, inifile)
  419.     ' discard trailing spaces and NULL character
  420.     strval = Left$(strval, valid)
  421.  
  422.     If strval = "*NO-VALUE*" Then
  423.         ' means INI file is unreadable or doesn't exist or key not found;
  424.         ' return empty string
  425.         strval = ""
  426.     End If
  427.  
  428.     GetIniValue = strval
  429.  
  430. End Function
  431.  
  432. Public Sub GetWizardInfo()
  433.  
  434.     ' only get a single argument:
  435.     ' path to temp INI file with arguments;
  436.     ' but have to handle quotes for installations
  437.     ' with spaces in path
  438.     
  439.     Dim cmd As String
  440.     Dim argfile As String
  441.     Dim idx As Integer
  442.     
  443.     cmd = Command
  444.     If (Left$(cmd, 1) = """") Then
  445.         idx = Len(cmd)
  446.         For idx = 2 To Len(cmd)
  447.             If Mid$(cmd, idx, 1) = """" Then
  448.                 argfile = Trim$(Mid$(cmd, 2, idx - 2))
  449.                 Exit For
  450.             End If
  451.         Next idx
  452.     Else
  453.         argfile = Trim$(cmd)
  454.     End If
  455.     
  456.     WizardArgFile = argfile
  457.         
  458.     ' fetch the built-in keys
  459.     If Len(WizardArgFile) > 0 Then
  460.         WizardDir = GetWizardInputVar(WizardDirKey)
  461.         WizardInf = GetWizardInputVar(WizardInfKey)
  462.         WizardBlocking = (CInt(GetWizardInputVar(WizardBlockingKey)) = 1)
  463.         WizardEditing = (CInt(GetWizardInputVar(WizardEditingKey)) = 1)
  464.         WizardPageURL = GetWizardInputVar(WizardPageURLKey)
  465.         WizardPageFile = GetWizardInputVar(WizardPageFileKey)
  466.         WizardPageTitle = GetWizardInputVar(WizardPageTitleKey)
  467.         WizardDestination = GetWizardInputVar(WizardDestinationKey)
  468.     End If
  469.     
  470.     ' establish the location for the wizard's persistent
  471.     ' state info (if any) in FrontPage data dir; use this
  472.     ' path if you need to save and restore settings from
  473.     ' the previous run of your wizard
  474.     If Len(FrontPageDataDir) > 0 Then
  475.         WizardIniFile = FrontPageDataDir & "\" & App.EXEName & ".ini"
  476.     Else
  477.         ' use wizard dir; better than nothing
  478.         WizardIniFile = App.path & "\" & App.EXEName & ".ini"
  479.     End If
  480.     
  481.     WizardHasPreviousSettings = FileExists(WizardIniFile)
  482.  
  483. End Sub
  484.  
  485. Public Function GetWizardInputVar(key As String) As String
  486.  
  487.     If Len(WizardArgFile) > 0 Then
  488.         GetWizardInputVar = GetIniValue(WizardArgFile, WizardInputSection, key)
  489.     Else
  490.         GetWizardInputVar = ""
  491.     End If
  492.  
  493. End Function
  494.  
  495. Public Sub SetIniValue(file As String, section As String, key As String, value As String)
  496.     
  497.     ' generic routine for setting an INI value in any file
  498.     
  499.     Dim ret As Long
  500.     
  501.     ret = WritePrivateProfileString(section, key, value, file)
  502.  
  503. End Sub
  504.  
  505. Public Sub SetWizardOutputVar(key As String, value As String)
  506.  
  507.     If Len(WizardArgFile) > 0 Then
  508.         SetIniValue WizardArgFile, WizardOutputSection, key, value
  509.     End If
  510.  
  511. End Sub
  512.  
  513. Public Function FileExists(path As String) As Boolean
  514.  
  515.     On Error GoTo NoSuchFile
  516.     Dim fileno As Integer
  517.     
  518.     fileno = FreeFile
  519.     Open path For Input As #fileno
  520.     Close #fileno
  521.     FileExists = True
  522.     Exit Function
  523.     
  524. NoSuchFile:
  525.     FileExists = False
  526.     Exit Function
  527.  
  528. End Function
  529.  
  530. Public Function GetWizardEnvironmentVar(key As String) As String
  531.  
  532.     ' use this routine to read environment variables as set
  533.     ' in the program that launched the wizard
  534.     GetWizardEnvironmentVar = GetIniValue(WizardArgFile, WizardEnvironmentSection, key)
  535.  
  536. End Function
  537.  
  538.  
  539.  
  540. Public Sub ValidatePageURLField(txtbox As TextBox)
  541.  
  542.     Dim inp As String
  543.     Dim outp As String
  544.     Dim sst As Integer
  545.     Dim slen As Integer
  546.     Dim i As Integer
  547.     Dim ch As String
  548.     Dim legals As String
  549.     Dim gotdot As Boolean
  550.     
  551.     ' alphanumeric
  552.     legals = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  553.     ' add the legal funny chars
  554.     legals = legals & "-_@$~&"
  555.     
  556.     sst = txtbox.SelStart
  557.     slen = txtbox.SelLength
  558.     
  559.     inp = txtbox.Text
  560.     
  561.     For i = 1 To Len(inp)
  562.         ch = Mid$(inp, i, 1)
  563.         If InStr(legals, ch) > 0 Then
  564.             outp = outp & ch
  565.         Else
  566.             If ch = "." Then
  567.                 gotdot = True
  568.                 Exit For
  569.             End If
  570.         End If
  571.     Next i
  572.     
  573.     If gotdot Then
  574.         outp = outp & ".htm"
  575.     Else
  576.         If Right$(outp, 3) = "htm" Then
  577.             outp = Left$(outp, Len(outp) - 3)
  578.             outp = outp & ".htm"
  579.         End If
  580.     End If
  581.         
  582.     If outp <> inp Then
  583.         txtbox.Text = outp
  584.         txtbox.SelStart = sst + (Len(outp) - Len(inp))
  585.     End If
  586.     
  587. End Sub
  588.  
  589. Public Sub SetWizardExitStatus(stat As String)
  590.  
  591.     SetWizardOutputVar WizardExitStatusKey, stat
  592.  
  593. End Sub
  594.  
  595. Public Sub SetWizardFileCount(num As Integer)
  596.  
  597.     SetWizardOutputVar WizardFileCountKey, CStr(num)
  598.  
  599. End Sub
  600.  
  601. Public Sub MakeFloatingWindow(hWnd As Long, yesno As Boolean)
  602.  
  603.     Dim ret As Long
  604.  
  605.     If yesno Then
  606.         ret = SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
  607.     Else
  608.         ret = SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
  609.     End If
  610.  
  611. End Sub
  612.