' Draft Scripting class 21 (7/24/2011)

' these examples cover the use, and creation of, shared objects

' example 1
' example showing the use of a sharedObject from the GW toolkit which is not
an object, but just a function

' This example returns a string containing the text "edit box"

dim objWindowType

set objWindowType =  SharedObjects.get("com.GWMicro.GWToolkit.WindowType")
' above, sharedObjects is a root level object which only has 3 methods: get,
register, and revoke

' the variable objWindowType, although it looks like we assigned it an
object, actually holds only a reference to a function

msgBox "The name for window type 12 is " & objWindowType(12)

' "get" is the default method of the sharedObjects object, and so you can
actually leave it off altogether as in the line below
set objWindowType =  SharedObjects("com.GWMicro.GWToolkit.WindowType")

' if you're using the "get" method of the sharedObjects object, below is a
much better form to use in your app

' "get" has an optional second parameter, which is the number of
milliseconds it is to wait for the object you want to become available;
' since use of "get"  will pause your app while waiting, it's best to
specify the wait time, so you can continue and let the user know a needed
shared object is not available

set objWindowType = nothing
set objWindowType =  SharedObjects.get("com.GWMicro.GWToolkit.WindowType",
10*1000) ' waits up to 10 seconds (the default is 30)
if objWindowType is nothing then
        msgBox "Could not get a needed shared object"

else
' here you go on to do whatever your app does knowing it got the shared
object it needs
end if

' end of example 1


' example 2
' here's the recommended way to get a shared object, by making use of the
event which tells you when each shared object is available.
' It also demonstrates use of the executeGlobal command.


Dim errorReportingEnabled 
errorReportingEnabled = False

' now connect to the onStateChange event of the sharedObjects object (which
is a root level property).
ConnectEvent SharedObjects, "OnStateChange", "HandleStateChange"

' end of main body

Function HandleStateChange(objName, objState)
' event handler for the onStateChange event of the sharedObjects object.
' the first parameter is the name of the object, and the second is true if
it's available, and false if not.

' this event gets triggered for each shared object  when your app starts, as
well as when new ones come online or go offline.

HandleStateChange = False

Select Case objName
' you only list CASE options for the objects you are interested in using
' even though you will receive a notification for all possible objects.

Case "com.GWMicro.GWToolkit.ErrorReporting"
  If objState Then
' object is available
' (here you put the commands specific to use of this object when it is
loaded)
    If Not errorReportingEnabled Then
      set objErrorReporting = SharedObjects(objName, 0) ' this too returns a
simple function and not an object
      strErrCommands = objErrorReporting(ClientInformation.ScriptVersion,
"[email protected]", True) ' which returns a string of VBScript commands
' the VBScript commands for error reporting primarily make use of the Script
object's onError event
      ExecuteGlobal strErrCommands
' the executeGlobal command takes a string of VBScript commands, and treats
them as if they were part of your app from the start; it inserts them into
your app
      errorReportingEnabled = True
    End If
  else
' object has become unavailable (maybe it's script has crashed)
' so maybe you do something in your script such as undefine a hotkey which
used this object, gray out some menu choices, whatever.
  end if

end select

HandleStateChange = True ' indicates you have handled this notification

end function

' end of example 2



' example 3:
' shows 3 different ways how to register your own object or function as a
shared object

' 1: below is how the GW toolkit handles it's windowType shared object
' register a function in your app as a shared object
                SharedObjects.Register "com.GWMicro.GWToolkit.WindowType",
GetRef("WindowType")

Function WindowType(typeNum)

' ...
end function


' 2: below is how the Homer library handles it's homer shared object
' register an object which is owned/created by the registering app 
' and truly this single object is shared by all apps.
' (this is ok because there are no private variables in the class which need
to be taylored to each app)

SharedObjects.Register "org.NonvisualDevelopment.Homer", New Homer

Class Homer
' Public methods

' Miscellaneous
Public Function Append(sText)
' Append a line of XML to a wescriptui definition

' If InStr(sText, "<") Or InStr(sText, ">") Or InStr(sText, "&") Or
InStr(sText, ";") Or InStr(sText, Chr(34)) Then Exit Function
If InStr(sText, "&") Or InStr(sText, ";") Or InStr(sText, Chr(34)) Then Exit
Function
sDialogXml = sDialogXml & sText & vbCrLf
End Function

' ...

end class




' 3: below is how the GW toolkit handles it's standardHelpDialog shared
object
' register an object, which contains a method for creating a "new" object
which the calling app must use to create the object it needs
' before it can be used.  Calling the method to create the new object gives
each calling app it's own copy of the object.
' (note: the two objects here are not of the same type.)
                SharedObjects.Register
"com.GWMicro.GWToolkit.StandardHelpDialog", New StandardHelpDialog
' notice the second parameter above is "new standardHelpDialog", which
creates an object of type "standardHelpDialog" which is what is registered
as the shared object,
' but there is only one of this object.
' when each user calls the newStandardHelpDialog method, they'll create
their own object to work with.

Class standardHelpDialog
   Public Function NewStandardHelpDialog()
' when the calling app calls this method, a new object with it's properties
and methods etc. will get created in the thread of the calling app, not the
registering app

      Set NewStandardHelpDialog = New standardHelpDialogClass
   End Function
End Class

' below is the class the user really works with, after creating a new object
of this type in their app by calling the .NewStandardHelpDialog method of
the
' shared object.
Class standardHelpDialogClass
        Public INIFileName
        Public INISectionName
        Public INIKeyName
        Public Hotkey
        Public HelpTitle
        Public HelpText
        Public KeyStrings
        Public ScriptName
        Public ScriptVersion
        Public UpdateUrl
        Public DefaultHotkeys
        Public FocusCloseButton
        Public UseAboutBox
        Public AboutAuthor
        Public AboutVersion
        Public AboutReleaseDate
        Public AboutCopyright
        Public AboutWebsite
        Public ShowHotkeysInHelp
        Public UseHotkeyManager
        Public ParentWindow

' ...

End Class

' end of example 3




' archives of these classes can be found at:
' https://www.gwmicro.com/App_Central/Developers/Interactive_Classes/

Reply via email to