Tooltips in the Datagrid

Bob Sneidar bobsneidar at iotecdigital.com
Fri Nov 10 16:54:52 EST 2017


Hi all. 

A while back I had a go at setting the tooltip when pointing at a datagrid element, but ran into some difficulties when scrolling. With Trevor Devore's assistance, I came up with a reliable way to do this. Enjoy. 

First, put this in your datagrid script:

on mouseEnter
   put the long id of the target into tTarget -- we need to preserver the original target
   put "sitename,addr1,city,state,zip,contactname,contactphone,contactemail," into tKeyList -- sample list of column names
   put "itname,itphone,itemail" after tKeyList -- adding more columns to the sample list
   dgToolTip tTarget, tKeyList
end mouseEnter

on mouseLeave
   put the long id of the target into tTarget -- we need to preserver the original target
   set the tooltip of tTarget to empty
end mouseLeave

NEXT put this into a backscript somewhere: 

on dgToolTip tTarget, tKeyList
   --    if the optionKey is up then
   --       set the tooltip of tTarget to empty
   --    exit dgToolTip
   --   end if
   
   put the name of tTarget into tTargetName
   
   if tTargetName contains "HeaderLabel" or the first word of tTargetName is not "field" then
      exit dgToolTip
   end if
   
   wait 30 ticks with messages
   put the name of the target into tNewTarget
   
   if tTargetName is not tNewTarget then -- what is being pointed at has changed
      put tNewTarget into tTargetname
      dgToolTip the target, tKeyList
      exit to top
   end if
   
   put the dgIndex of the dgDataControl of tTarget into tIndex -- thanks for this Trevor! 
   
   replace quote with empty in tTargetName
   put the last word of tTargetName into tLine
   put the dgDataOfIndex [tIndex] of tTarget into aLineData
   
   if tKeyList is empty or the shiftKey is down then
      put the keys of aLineData into tKeyList
      sort lines of tKeyList ascending
      replace cr with comma in tKeyList
   end if
   
   set the numberformat to "0000"
   
   repeat for each item tKey in tKeyList
      add 1 to tCounter
      put "field " & tCounter into tFieldName
      put aLineData [tKey] into aRecordData [tCounter & ". " & tKey]
   end repeat
   
   put arrayToText(aRecordData, "record") into tText
   sort lines of tText numeric ascending by word 1 of each
   
   set the tooltip of tTarget to tText
end dgToolTip

function arrayToText aArrayData, pFormat, pStripRecordKeys
   if aArrayData is not an array then
      put "ERROR: Data passed is not an array!" into theValue
      return theValue
   end if
   
   if pStripRecordKeys is not in "true|false" then put false into pStripRecordKeys
   -- sort tArrayKeys ascending
   
   -- may be an array of values
   put the keys of aArrayData into tArrayKeys
   sort lines of tArrayKeys numeric ascending
   put line 1 of tArrayKeys is 0 into tHasHeader
   
   -- convert single array to numeric array of arrays
   if line 1 of tArrayKeys is not a number then
      put aArrayData into aTempArray [1]
      put aTempArray into aArrayData
      put the keys of aArrayData into tArrayKeys
   end if
   
   switch pFormat
      case "record"
         repeat for each line tArrayKey in tArrayKeys
            put aArrayData [tArrayKey] into aArrayRecord
            put the keys of aArrayRecord into tColumnKeys
            
            repeat for each line tColumnKey in tColumnKeys
               put tColumnKey & ": " & aArrayRecord [tColumnKey] & cr after tValue
            end repeat
         end repeat
         break
      case "table"
         -- table header
         if not tHasHeader then put cr into tValue
         
         repeat for each line tArrayKey in tArrayKeys
            add 1 to tCount
            put aArrayData [tArrayKey] into aArrayRecord
            put the keys of aArrayRecord into tColumnKeys
            sort lines of tColumnKeys numeric ascending
            
            repeat for each line tColumnKey in tColumnKeys
               if pStripRecordKeys and (char 1 of tColumnKey is "@") then next repeat
               
               if tCount = 1 and not tHasHeader then put tColumnKey & tab after line 1 of tValue
               put aArrayRecord [tColumnKey] & tab after tValue
            end repeat
            
            put cr into last char of tValue
         end repeat
         break
   end switch
   
   return tValue
end arrayToText






More information about the use-livecode mailing list