templates and instances for custom controls (was: object scripts)

Dick Kriesel dick.kriesel at mail.com
Mon Feb 27 21:40:01 EST 2006


On 2/26/06 11:41 AM, "Geoff Canyon" <gcanyon at inspiredlogic.com> wrote:

> The Object Oriented way to do this is to define a new group type:
> alertGroup, say. In the definition of the alertGroup, you include the
> original mouseUp handler. Then any time you need a group that has
> this behavior you declare it to be an alertGroup (instead of a
> generic group). In every way it acts like a normal group, but when a
> control in it is clicked, it displays a dialog with the name of the
> control. If you need to make it a warning dialog, change the class
> definition and the behavior everywhere changes.

Here's a Transcript-based technique for treating any custom control as a
template, by creating and deleting instances.  Each instance invokes
handlers stored only in the template, by executing a Transcript "call."
Here's an example:

on mouseDown
  try
    call the params of the uTemplate of the target
  end try
  pass mouseDown
end mouseDown

All the system message handlers like the above are stored in the instance's
top-level script.  No component control within the instance has a script.
Every control in the instance has property uTemplate.  A template can reside
in a library, but doesn't need to.  A template can override or supplement
the instance handlers like the example above, by setting properties in the
template's uInstanceScript custom property set.  Templates and instances
refer to each other using "rugged" long ids, which are native long ids
stripped of any "of group id" clause.  Rugged ids don't fail when the
developer moves a control to a different owner.

To set that up, the developer creates an instance by sending a message to
the template, passing a reference to the group that will be the owner of the
new instance:

send "template_createInstance tGroup" to tCustomControl

That's it for the overview.  Here are most of the details:

on template_createInstance pOwner
  if there is a group pOwner then
    put the uRuggedLongID of the target into tTemplate
    -- note: the template can be in a library stack
    copy tTemplate to group pOwner
    put the uRuggedLongID of it into tInstance
    set the uInstances of tTemplate to \
        the uInstances of tTemplate & tInstance & cr
    -- note: the uInstances can guide a developer to propagate \
    --       template changes out to the instances
    set the uTemplate of it to tTemplate
    set the script of it to the uInstanceScript of tTemplate
    repeat with i = 1 to number of controls in it
      set the uTemplate of control i of it to \
          the uRuggedLongID of control i of tTemplate
      set the script of control i of it to empty
    end repeat
    send "choose browse tool" to me in 0
  else
    breakPoint
  end if
end template_createInstance

on template_deleteInstance
  put the uTemplate of the target into tTemplate
  if tTemplate is not empty then
    put the uInstances of tTemplate into tInstances
    filter tInstances without the uRuggedLongID of the target
    set the uInstances of tTemplate to tInstances
  end if
end template_deleteInstance

getProp uRuggedID
  put the long id of the target into tLongID
  if word 1 of tLongID is not "stack" then
    -- note: stacks do not have rugged ids
    put word 1 to 3 of tLongID into tRuggedID
    put offset(" of card id " & quote,tLongID) into tOffset
    if tOffset > 0 then
      put char tOffset to offset(space,tLongID,tOffset) - 1 of tLongID \
          after tRuggedID
    end if
  end if
  return tRuggedID
end uRuggedID

getProp uRuggedLongID
  put the uRuggedID of the target into tRuggedLongID
  put offset(" of stack " & quote,the long id of the target) into tOffset
  put char tOffset to -1 of the long id of the target after tRuggedLongID
  return tRuggedLongID
end uRuggedLongID

getProp uInstanceScript
  lock messages
  put the uInstanceScript of the target into tInstanceScript
  put cr & the uInstanceScript[word 1 of the target] of me \
      after tInstanceScript
  unlock messages
end uInstanceScript

All the above handlers could be stored in a library, which could be called
something like libTemplate, and which I could produce if people are
interested.

Any feedback?

-- Dick





More information about the use-livecode mailing list