home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 26 / CD_ASCQ_26_1295.iso / vrac / holcal.zip / HOLLINK.BAS < prev    next >
BASIC Source File  |  1995-08-06  |  9KB  |  209 lines

  1. ' HolLink.Bas - Subroutine to Link to the Holiday Calendar
  2. ' 95/02/19 Copyright 1995, Larry Rebich, The Bridge, Inc.
  3.  
  4.     Option Explicit
  5.     DefInt A-Z
  6.     
  7.     Declare Function HCL_GetPrivateProfileString Lib "Kernel" Alias "GetPrivateProfileString" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
  8.     Declare Function HCL_GetWindowsDirectory Lib "Kernel" Alias "GetWindowsDirectory" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
  9.  
  10. ' Global Constants and Variables used by the LinkCalendar subroutine
  11.     Global Const HC_HolidayCalendarExe = "HolCal.Exe"
  12.     Const HolidayCalendarIniFile = "HolCal.Ini"
  13.     
  14. ' Local Constants
  15.     Const HolidayCalendarNoExe = "HolCal"
  16.     Const HolidayCalendarFormName = "FormCalendarV3"
  17.     Const HolidayCalendar = "Holiday Calendar"
  18.     Const HolidayCalendarEnd = "@ End the Holiday Calendar @"
  19.     Const HolidayCalendarActivate = "+ Activate the Holiday Calendar +"
  20.     Const HolidayCalendarMinimize = "- Minimize the Holiday Calendar -"
  21.  
  22. ' Link Constants
  23.     Const NONE = 0           ' 0 - None
  24.     Const LINK_AUTOMATIC = 1 ' 1 - Automatic (controls only)
  25.     Const LINK_MANUAL = 2    ' 2 - Manual (controls only)
  26.  
  27. ' Holiday Calendar Action Constants, use in your application
  28.     Global Const HCL_LINKIFSTARTED = &H0    'link of already running
  29.     Global Const HCL_FORCESTART = &H2       'force a shell if Calendar not already running
  30.     Global Const HCL_CLOSECALENDAR = &H4    'send closing message, if started
  31.     Global Const HCL_MINIMIZE = &H8         'send to icon state, if started
  32.  
  33. Function HCL_Delay (Secs As Single)
  34.     Dim EndTime As Double
  35. 'convert seconds to percent of day and add to current day/time
  36.     EndTime = Now + (Secs / 86400#)
  37.     While Now < EndTime     'delay for a while
  38.     Wend
  39. End Function
  40.  
  41. Private Function HCL_GetCalendarPathFromIni (TheIniFile As String) As String
  42.     Dim a As String         'work string
  43.     Dim b As String         'work string
  44.     Dim hp As String        'holiday path
  45.     Dim ap As String        'app path
  46.     Dim wp As String        'win dir
  47.     wp = HCL_GetWinDir()    'get win dir
  48.     ap = App.Path           'app path, try here if not in WinDir
  49.     If Right$(ap, 1) <> "\" Then ap = ap & "\"
  50.     a = wp & TheIniFile
  51.     If Dir$(a) = "" Then            'is there one
  52.         a = ap & TheIniFile         'look in this one
  53.     End If
  54.  
  55.     If Dir$(a) <> "" Then
  56.         Dim OK As Integer
  57.         Dim Value As Variant
  58.         OK = HCL_IniGetPrivate("Files", "Application Path", Value, a)
  59.         If OK Then
  60.             hp = Value
  61.             If Right$(hp, 1) <> "\" Then hp = hp & "\"
  62.         Else
  63.             hp = ap     'try this one, however link will fail if it is not there
  64.         End If
  65.     Else
  66.         a = Dir$(ap & HC_HolidayCalendarExe)
  67.         If a <> "" Then
  68.             hp = ap
  69.         Else
  70.             Dim Msg As String
  71.             Msg = "The " & HolidayCalendar & " initialization file could not be "
  72.             Msg = Msg & "found in " & LCase$(Left$(wp, Len(wp) - 1))
  73.             Msg = Msg & " or " & LCase$(Left$(ap, Len(ap) - 1)) & ""
  74.             Msg = Msg & " and " & HC_HolidayCalendarExe & " is not in " & LCase$(Left$(ap, Len(ap) - 1))
  75.             Msg = Msg & "." & Chr$(13) & Chr$(13)
  76.             Msg = Msg & "Please run " & HC_HolidayCalendarExe & " then try this again."
  77.             MsgBox Msg, 16, "Could not find " & TheIniFile & " or " & HC_HolidayCalendarExe
  78.             End
  79.         End If
  80.     End If
  81.     HCL_GetCalendarPathFromIni = hp
  82. End Function
  83.  
  84. Private Function HCL_GetWinDir () As String
  85.     Dim Temp As String          'get the windows directory
  86.     Dim nSize, ValidLength
  87.     Dim lpBuffer As String
  88.     nSize = 150                 'buffer size
  89.     lpBuffer$ = Space$(nSize)   'load buffer with spaces
  90.     ValidLength = HCL_GetWindowsDirectory(lpBuffer$, nSize) 'get it
  91.     Temp = Trim$(Left$(lpBuffer$, ValidLength)) 'into temp
  92.     If Right$(Temp, 1) <> "\" Then Temp = Temp & "\"
  93.     HCL_GetWinDir = Temp
  94. End Function
  95.  
  96. Private Function HCL_IniGetPrivate (AppName As String, Key As String, Value As Variant, IniFile As String) As Integer
  97.     Dim Buf As Integer, Num As Integer
  98.     Dim SValue As String    'loaded into here
  99.     Buf = 64
  100.     SValue = Space$(Buf)
  101.     Num = HCL_GetPrivateProfileString(AppName, Key, "", SValue, Buf, IniFile)
  102.     If Num > 0 Then     'OK?
  103.         Value = Trim$(Mid$(SValue, 1, Num))
  104.         HCL_IniGetPrivate = True
  105.     Else
  106.         Value = ""          'return nothing
  107.     End If
  108. End Function
  109.  
  110. Function HCL_LinkToCalendar (TheBoxDate As Control, TheLabelStatus As Control, TheLabelMessage As Control, TheAction As Integer) As Integer
  111. ' Call this Function to initiate a DDE link with the Holiday Calendar
  112. '
  113. '       TheBoxDate              'Date passed in this text box
  114. '       TheLabelStatus          'Status from the Holiday Calendar
  115. '       TheLabelMessage         'Message from this subroutine
  116. '       TheAction               'Action to be taken
  117. '
  118. ' Returns True if function was performed.
  119. '
  120. ' A Sample:
  121. '   Rtn = HC_LinkToCalendar(Text1, Label1, Label2, HCL_FORCESTART)
  122.  
  123.     Static WeStartedIt As Integer   'save this, only terminate the calendar if we started it
  124.     Dim a As String, b As String, x As Integer
  125.     Dim h As String                 'link topic, easier to work with
  126.     h = HolidayCalendarNoExe & "|" & HolidayCalendarFormName                'link topic
  127.     ' minimize
  128.     If Hex(TheAction And HCL_MINIMIZE) = Hex(HCL_MINIMIZE) Then
  129.         TheLabelStatus.Caption = HolidayCalendarMinimize    'yes
  130.         On Error Resume Next
  131.         TheLabelStatus.LinkPoke     'send the cancel command
  132.         x = HCL_Delay(.3)           'pause a bit
  133.         HCL_LinkToCalendar = True   'say it was completed
  134.         Exit Function                   'bye
  135.     End If
  136.     ' close
  137.     If Hex(TheAction And HCL_CLOSECALENDAR) = Hex(HCL_CLOSECALENDAR) Then   'does user want to terminate it
  138.         If WeStartedIt Then         'did we start it
  139.             TheLabelStatus.Caption = HolidayCalendarEnd     'yes
  140.             On Error Resume Next
  141.             TheLabelStatus.LinkPoke     'send the cancel command
  142.             x = HCL_Delay(.3)           'pause a bit
  143.             HCL_LinkToCalendar = True   'say it was completed
  144.         End If
  145.         Exit Function                   'bye
  146.     End If
  147.     On Error GoTo TryShell              'in case linkage fails
  148.     TheLabelMessage.Caption = "Establish DDE Link"
  149. TryAgain:
  150.     TheBoxDate.LinkMode = NONE          'reset if any
  151.     TheBoxDate.LinkTopic = h            'topic
  152.     TheBoxDate.LinkTimeout = 10 * 30    'thirty seconds should be enough
  153.     TheBoxDate.LinkItem = "TextLinkCalendar"    'link to this box
  154.     TheBoxDate.LinkMode = LINK_MANUAL           'first time
  155.     TheLabelMessage.Caption = "Link Date Field" 'message to user
  156.     TheLabelMessage.Refresh                     'make it visible
  157.     DoEvents
  158.     'set up status link
  159.     TheLabelStatus.LinkMode = NONE      'reset if any
  160.     TheLabelStatus.LinkTopic = h        'link the status box
  161.     TheLabelStatus.LinkTimeout = TheBoxDate.LinkTimeout
  162.     TheLabelStatus.LinkItem = "TextLinkStatus"
  163.     TheLabelStatus.LinkMode = LINK_AUTOMATIC        'updates are automatic
  164.     TheLabelMessage.Caption = "Link Status Field"   'tell user
  165.     On Error GoTo TryShell
  166.     TheBoxDate.LinkPoke                             'force it to source
  167.     TheBoxDate.LinkMode = LINK_AUTOMATIC            'date box is now automatic
  168.     TheLabelMessage.Caption = "Link Established"    'tell user
  169.     TheLabelStatus.Caption = HolidayCalendarActivate'Starting
  170.     On Error Resume Next
  171.     AppActivate TheBoxDate.Text
  172.     TheLabelStatus.LinkPoke                         'send the start command
  173.     x = HCL_Delay(.2)                               'pause a bit
  174.     DoEvents
  175.     HCL_LinkToCalendar = True           'completed
  176.     Exit Function                       'bye
  177.  
  178. TryShell:
  179.     If Hex(TheAction And HCL_FORCESTART) = Hex(HCL_FORCESTART) Then 'caller does not want to force shell
  180.     Else
  181.         Exit Function
  182.     End If
  183.     x = Err                         'save for later
  184.     a = HCL_GetCalendarPathFromIni(HolidayCalendarIniFile)  'get calendar path
  185.     a = a & HC_HolidayCalendarExe   'is it there?
  186.     b = Trim$(Dir$(a))              'use Dir$ function to test
  187.     If b = "" Then                  'is it there
  188.         x = 0                       'if not there then set error msg to zero
  189.     End If
  190.     If x = 282 Or x = 293 Then      'error was no link
  191.         TheLabelMessage.Caption = "Shell to " & LCase$(a)
  192.         TheLabelStatus.Caption = ""
  193.         x = Shell(a, 1)             'try to load it
  194.         WeStartedIt = True          'will not get here if shell fails
  195.         DoEvents
  196.         On Error GoTo LinkFailed    'reset error goto
  197.         Resume TryAgain             'try again
  198.     Else
  199.         MsgBox "Could not start file: " & a, 48, Str$(Err) & " - " & Error$
  200.         Resume ExitThis             'problem and bye
  201.     End If
  202. LinkFailed:                         'could not link after attempting shell
  203.     TheLabelMessage.Caption = "Link Failed - " & Error$
  204.     MsgBox "Link Failed", 48, Str$(Err) & " - " & Error$
  205.     Resume ExitThis
  206. ExitThis:
  207. End Function
  208.  
  209.