
command FitText Obj , TextSizeScaleFactor , XorYfit, NoMarginReset
   
   --Fit text into a control
   --Calling squence
   -- FitText Obj , TextSizeScaleFactor , XorYfit
   -- Obj==>Short name of a control
   -- Scale factor in percent 0-100
   -- Fit based on on a single dimension   
   
   local LinesOfText 
   local FieldMaxWidthLine
   local doX , doY
   
   try
      put true into doX
      put true into doY 
      
      if XorYfit is not empty then
         switch XorYfit
            case "X"
            case "x"
               put false into doY
               break
            case "Y"
            case "y"
               put false into doX
               break
         end switch
      end if
      
      put getlongname(obj) into obj
      try
         if not NoMarginReset then
            set the margins of obj to 0
         end if
      end try
      Switch word 1 of obj
         case "button"
            switch the style of Obj
               case "Menu"
                  put 1 into LinesOfText
                  put 1 into FieldMaxWidthLine
                  break
               default
                  If the label of obj is empty then exit FitText
                  put the number of lines in the label of obj into LinesOfText
                  put FindWidestLine (obj,LinesOfText) into FieldMaxWidthLine
                  if FieldMaxWidthLine < 1 then exit FitText
            end switch
            break
         Case "field"
            If the text of obj is empty then exit FitText
            put the number of lines in the text of obj into LinesOfText
            put 1 into FieldMaxWidthLine
            if FieldMaxWidthLine < 1 then exit FitText
            break
         default
            put the number of lines in the text of obj into LinesOfText
            put 1 into FieldMaxWidthLine
            break
      end Switch
      
      Switch word 1 of obj
         case "button"
            FitTextButton Obj  , Dox , DoY , LinesOfText
            break
         default
            put 0 into NumFitText
            FitTextNotButton Obj , DoX , DoY
            break
      end switch
     
      if TextSizeScaleFactor is not empty then
         Switch word 1 of obj
            case "button"
               set the textsize of Obj to round(( (the textsize of Obj) * ( TextSizeScaleFactor / 100) ))
               break
            default
               set the textsize of Obj to ( (the textsize of Obj) * ( TextSizeScaleFactor / 100) )
               break
         end switch
      end if
   end try
   
end FitText

command FitTextNotButton Obj , Dox , DoY , cForceEffectiveHeight
   local scrollsize, tBorderWidth
   local tLock
   
   if the lockscreen then
      put false into tLock
   else
      put true into tLock
   end if
   if tLock then lock screen
   
   if the showborder of Obj then put the borderwidth of Obj into tBorderwidth
   
   set the textsize of Obj to the height of obj --+  (the height of obj * .1)
   set the textsize of line 1 to -1 obj  to empty
  
   try
      if the hscrollbar of obj or vscrollbar of obj then
         put the scrollbarwidth of obj into scrollsize
      else
         put 0 into scrollsize
      end if
      
      local ObjWidth , vTabs
      if the vgrid of Obj then
         put the tabstops of obj into vTabs
         sort items of vTabs
         put item 1 of vtabs into ObjWidth
      else
         put the width of Obj into ObjWidth
      end if
      if the showborder of Obj then
         subtract the borderwidth of Obj * 2 from ObjWidth
      end if
      
      
      repeat while ( ( ((the formattedwidth of Obj)  > ((ObjWidth-(scrollsize))/*-(borderwid(obj)*2)*/) and Dox) or ((ControlTextHeight( Obj,cForceEffectiveHeight)*1)) > ((the height of Obj) -(tBorderwidth)) and Doy )  and ( the textsize of obj  > 4) )
         set the textsize of obj  to (the textsize of Obj) - 4
      end repeat
      set the textsize of obj  to (the textsize of Obj) + 3
      repeat while ( ( ((the formattedwidth of Obj)  > ((ObjWidth-(scrollsize))/*-(borderwid(obj)*2)*/) and Dox) or ((ControlTextHeight( Obj,cForceEffectiveHeight)*1)) > ((the height of Obj) -(tBorderwidth)) and Doy )  and ( the textsize of obj  > 2) )
         set the textsize of obj  to (the textsize of Obj) - 2
      end repeat
      set the textsize of obj  to (the textsize of Obj) + 1
      repeat while ( ( ((the formattedwidth of Obj)  > ((ObjWidth-(scrollsize))/*-(borderwid(obj)*2)*/) and Dox) or ((ControlTextHeight( Obj,cForceEffectiveHeight)*1)) > ((the height of Obj) -(tBorderwidth)) and Doy )  and ( the textsize of obj  > 1) )
         set the textsize of obj  to (the textsize of Obj) - 1 
      end repeat
      
      if (the textsize of Obj <= 4) and (NumFitText < 1)  then
         add 1 to NumFitText
         FitTextNotButton Obj , Dox , DoY , true--(the effective textheight of obj)
      end if
      if fixedlineheight of obj then set the textheight of obj to (the height of obj / the number of lines of text of obj) * .95
   end try
   if tLock then unlock screen
end FitTextNotButton

Command FitTextButton  Obj , DoX , DoY , LinesOfText
   local scrollsize
   local tLock
   
   if the lockscreen then
      put false into tLock
   else
      put true into tLock
   end if
   if tLock then lock screen
   
   if the number of lines in the label of obj = 1 then
      set the textsize of Obj to the height of obj-- + (the height of obj * .1)
      repeat while ( ( ((the effective formattedwidth of Obj/linesoftext)  > ((width of Obj)-(borderwid(obj)*2)) and DoX ) or ( (ControlTextHeight( Obj)*LinesOfText) ) > ( (the height of Obj) -(the borderwidth of obj*0)) and Doy )  and ( the textsize of obj  > 4) )
         set the textsize of obj  to round((the textsize of Obj) - 4)
      end repeat
      set the textsize of obj  to (the textsize of Obj) + 3
      repeat while ( ( ((the effective formattedwidth of Obj/linesoftext)  > ((width of Obj)-(borderwid(obj)*2)) and DoX ) or ( (ControlTextHeight( Obj)*LinesOfText) ) > ( (the height of Obj) -(the borderwidth of obj*0)) and Doy )  and ( the textsize of obj  > 2) )
         set the textsize of obj  to round((the textsize of Obj) - 2)
      end repeat
      set the textsize of obj  to (the textsize of Obj) + 1
      repeat while ( ( ((the effective formattedwidth of Obj/linesoftext)  > ((width of Obj)-(borderwid(obj)*2)) and DoX ) or ( (ControlTextHeight( Obj)*LinesOfText) ) > ( (the height of Obj) -(the borderwidth of obj*0)) and Doy )  and ( the textsize of obj  > 1) )
         set the textsize of obj  to round((the textsize of Obj) - 1)
      end repeat
   else
      try
         repeat forever
            delete control "TextSizeCalc"
         end repeat
      end try
      
      try
         create invisible field "TextSizeCalc"
         if the style of Obj = "menu" then
            put the text of obj into field "TextSizeCalc"
         else
            put the label of obj into field "TextSizeCalc"
         end if
         set the margins of field "TextSizeCalc" to the margins of obj
         set the textstyle of field "TextSizeCalc" to the textstyle of obj
         set the textfont of field "TextSizeCalc" to the textfont of obj
         set the rect of control "TextSizeCalc" to the rect of obj
         fittext "TextSizeCalc"
         set the textsize of obj to the textsize of control "TextSizeCalc"
         delete control "TextSizeCalc"
      end try
      if tLock then unlock screen
   end if
   
end FitTextButton

function ControlTextHeight Obj , ForceEffectiveHeight
   
   try
      put getlongname(obj) into obj
      Switch word 1 of obj
         case "Button"
            return (the effective textheight of obj)
            break
         default
            return (the formattedheight of obj)
           
            if ForceEffectiveHeight then return (the effective textheight of obj)
            return (max(the formattedheight of obj,the effective textheight of obj))
            break
      end switch
   end try
   
   return empty
   
end ControlTextHeight

function FindWidestLine obj , LinesOfText
   local FieldMaxWidth , FieldMaxWidthLine
   
   if LinesOfText < 1 then
      return 0
   else
      put 1 into FieldMaxWidthLine
      put 0 into FieldMaxWidth
      repeat with I = 1 to linesoftext
         switch word 1 of obj
            case "Button"
               break
            default
               if the formattedwidth of line I of obj > FieldMaxWidth then
                  put I into FieldMaxWidthLine
                  put the formattedwidth of line I of obj into FieldMaxWidth
               end if
               break
         end switch
      end repeat
      return FieldMaxWidthLine
   end if
end FindWidestLine

function GetLongName , pObj
   local Obj
   if not (pObj contains quote) then put quote&pObj&quote into pObj
   if ("of card" is not among the chars of pobj) and ("of stack" is not among the chars of pobj) then
      try
         put the long name of ("control"&& pObj) into Obj
      catch SomeError
         try
            put the long name of control pObj into Obj
         end try
      end try
   else
      try
         put the long name of pObj into Obj
      end try
   end if
   return Obj
end GetLongName