[comp.sys.mac.hypercard] Making areas of field text clickable

KOFOID@cc.utah.edu (02/15/90)

With regard to the recent question about double-clicking in field text:

Place the following scripts into any field to make double-clickable buttons out
of designated words. I've designed the scripts to key on "dill", "sweet" and
"pickle", which should be mixed in with other words in the field to make the
test complete.

==========================================

on mouseUp
  wait 5
  if the mouse is down then
    put the clickLoc into thePoint
    set the lockText of me to false
    click at thePoint
    click at thePoint
    if "on"&&(the selection)&return is in the script of me then
      send the selection to me
    else
      put "'"&(the selection)&"'"&&"is not a button."
    end if
    click at thePoint
    set the lockText of me to true
  end if
end mouseUp

on openField
  wait 5
  if the mouse is down then
    click at the clickLoc
    click at the clickLoc
    if "on"&&(the selection)&return is in the script of me then
      send the selection to me
      put the result
    else
      put "'"&(the selection)&"'"&&"is not a button."
    end if
  end if
end openField

on pickle
  visual effect barn door close very slowly to black
  go to this cd
end pickle

on dill
  visual effect zoom close very slowly to gray
  go to this cd
end dill

on sweet
  visual effect dissolve very slowly to black
  go to this cd
end sweet

=====================================

Eric Kofoid

 Snail: Dept. Biology/U. Utah/S.L.C., Utah 84103
  Good: bi.kofoid%science@utahcca
Better: bi.kofoid@science.utah.edu
  Best: kofoid@bioscience.utah.edu

KOFOID@cc.utah.edu (02/17/90)

      David Neves writes:

>I have many field texts.  I want to make most of the text in these >fields
sensitive to clicks.  The 2 ways of doing this are:
>
>1.  Have the field (on mousedown or mouseup) figure out what text the user 
>has clicked on.  This gets messy because the only RETURNs in the fields are
>between paragraphs and the font is variable width.<...etc.>
>
>2.  Put buttons over all the text.<...etc.>

      I answered yesterday with a simple script for capturing the text of
individual words, which I use repeatedly in many of my stacks. On thinking
about David's request, however, I realize that he probably wants to capture
typographical lines (i.e., starting at left of the field rect and ending at
right), or actual sentences ("lines" as used by HyperCard). Both are also easy
to do. I've appended scripts for doing all three (words, lines, sentences)
somewhat embellished so as to be self testing.

      In general, I try to avoid the mouseWithin message, as suggested by
Jeanne DeVoto, as its handlers are slower than those for mouseUp or openField.
The latter also lend themselves easily to double-click recognition. mouseWithin
handlers are very erratic about trapping mouse events. On the other hand, my
code is twice as long, as I must have handlers triggered by either value of
lockText. Clearly, if you know ahead of time the status of the text will be
permanently locked or unlocked, then you can excise the inappropriate handler.

      I use 5 ticks as the double click window. This can be changed, and
probably ought be initialized in a global variable on opening the stack. 

      I hope this helps.

            Cheers,

                  Eric.

==========

-- words as buttons:

on mouseUp
  wait 5
  if the mouse is down then
    put the clickLoc into thePoint
    set the lockText of me to false
    getButton
    click at thePoint
    set the lockText of me to true
  end if
end mouseUp

on openField
  wait 5
  if the mouse is down then
    getButton
  end if
end openField

on getButton
  click at the clickLoc
  click at the clickLoc
  if "on"&&(the selection)&return is in the script of me then
    send the selection to me
  else
    put "'"&(the selection)&"'"&&"is not a button."
    wait 30
    hide msg
  end if
end getButton

on pickle
  visual effect barn door close very slowly to black
  go to this cd
end pickle

on dill
  visual effect zoom close very slowly to gray
  go to this cd
end dill

on sweet
  visual effect dissolve very slowly to black
  go to this cd
end sweet

==========

-- lines as buttons

on mouseUp
  wait 5
  if the mouse is down then
    set the lockText of me to false
    getLine
    set the lockText of me to true
  end if
end mouseUp

on openField
  wait 5
  if the mouse is down then
    getLine
  end if
end openField

on getLine
  put the clickLoc into startClick
  put item 2 of the clickLoc into theY
  put item 1 of rect of me into theLeft
  if the style of me is "scrolling" then put 18 into delta
  else put 1 into delta
  put ((item 3 of rect of me)-delta) into theRight
  click at (theLeft&","&theY)
  click at (theRight&","&theY) with shiftKey
  put the selection into theLine
  activateLine theLine
  click at startClick
end getLine

on activateLine aLine
  if aLine contains "doodly" then
    put aLine
    visual effect iris close very slowly to black
    go to this cd
  else if aLine contains "& a duck" then
    put aLine
    visual effect barn door close very slowly to gray
    go to this cd
  else if aLine contains "dill pickle" then
    put aLine
    visual effect dissolve very slowly to black
    go to this cd
  else
    put "Line button not found"
  end if
  wait 60
  hide msg
end activateLine

==========

-- sentences as buttons

on mouseUp
  wait 5
  if the mouse is down then
    set the lockText of me to false
    click at the clickLoc
    getPhrase
    set the lockText of me to true
  end if
end mouseUp

on openField
  wait 5
  if the mouse is down then
    getPhrase
  end if
end openField

on getPhrase
  select the selectedLine
  put the selection into thePhrase
  activatePhrase thePhrase
end getPhrase

on activatePhrase aPhrase
  if aPhrase contains "doodly" then
    put aPhrase
    visual effect iris close very slowly to black
    go to this cd
  else if aPhrase contains "& a duck" then
    put aPhrase
    visual effect barn door close very slowly to gray
    go to this cd
  else if aPhrase contains "dill pickle" then
    put aPhrase
    visual effect dissolve very slowly to black
    go to this cd
  else
    put "Phrase button not found"
  end if
  wait 60
  hide msg
end activatePhrase

==========

Eric Kofoid

 Snail: Dept. Biology/U. Utah/S.L.C., Utah 84103
  Good: bi.kofoid%science@utahcca
Better: bi.kofoid@science.utah.edu
  Best: kofoid@bioscience.utah.edu