' StringGadget: Alan Swithenbank, February 1991

' Open a normal window connected to WorkBench which contains a string gadget.
  
  CLEAR ,25000
  CLEAR ,30000

  DEFLNG a-Z  
 
  LOCATE 10,10
  PRINT "Reading Parameter Value Overlay"
  CHAIN MERGE "j:Structures4/AmigaBASIC.h",9000,ALL
  
AmigaBASICReturn:
  
  LOCATE 10,10
  PRINT "Reading Structure Construction Subprogram Overlay"
  CHAIN MERGE "j:Structures4/StructSubs.h",9003,ALL,DELETE 9000-
  
StructSubsReturn:

  LOCATE 10,10
  PRINT "Reading Structure Definition Subprogram Overlay  "
  CHAIN MERGE "j:Structures4/StrctSubII.h",9005,ALL
  
StrctSubIIReturn:
    
  LIBRARY "j:structures4/exec.library"
    DECLARE FUNCTION AllocMem() LIBRARY
  LIBRARY "j:structures4/intuition.library"
    DECLARE FUNCTION OpenWindow() LIBRARY
    DECLARE FUNCTION WaitPort() LIBRARY   
    DECLARE FUNCTION GetMsg() LIBRARY
  LIBRARY "j:structures4/graphics.library"
    
'**** allocate some space for StringInfo structure buffers: 
 
  STRUCT MyBuffer,100&,"ClrPubFAST"
  MyUndoBuffer = MyBuffer + 50&
  
'**** set up StringInfo Structure:

  MB = MyBuffer
  UB = MyUndoBuffer
  
  StringInfo MyStringInfo,MB,UB,0&,50&,0&,0&,0&,0&,0&,0&,0&,0&,0&
   
'**** read in coordinate-pairs and set up Border structure:

  NumPairs = 5
  NumBytes = NumPairs*4&

  STRUCT MyCoords,NumBytes,"ClrPubFAST"
  
  ReadImage MyCoords,NumBytes
     
  Border MyBorder,0&,0&,1&,0&,JAM1,NumPairs,MyCoords,0&
 
'**** set up IntuiText structure:

  IntuiText MyText,1&,0&,JAM1,-53&,0&,0&,"Text:",0& 
   
'**** set up Gadget structure:
   
  GF = GADGHCOMP
  GA = GADGIMMEDIATE + RELVERIFY
  GT = STRGADGET
  GR = MyBorder
  T  = MyText
  SI = MyStringInfo
 
  Gadget MyGadget,0&,68&,30&,196&,8&,GF,GA,GT,GR,0&,T,0&,SI,0&,0&
 
'**** set up Window Structure:

  L  = 50&
  T  = 25&
  W  = 320&
  H  = 100&
  IM = CLSWINDOW + GADGETDOWN + GADGETUP
  FM = WINDOWCLOSE + WINDOWDRAG + WINDOWDEPTH + WINDOWSIZING + ACTIVATE
  G  = MyGadget
  T$ = "String Gadget" 
  X  = 320&
  Y  = 50&
  U  = 640&
  V  = 200&
       
  xWindow MyWindow,L,T,W,H,0&,1&,IM,FM,G,0&,T$,0&,0&,X,Y,U,V,WBENCHSCREEN

  CLS
 
  LOCATE 5,48
  PRINT "When done quit with program"
  LOCATE  ,48
  PRINT "window close gadget."

'**** Set up and use IDCMP:

  MyRastPort = WINDOW(8)  ' current window is AmigaBASIC default

  UserPort = 86 ' offset to UserPort pointer in Window structure
  
  MyUserPort = PEEKL(MyWindow+UserPort) ' pointer to user MsgPort structure 
  
  CloseMe = FALSE
  
  WHILE NOT CloseMe

WaitAgain:
  
    MyMessage = WaitPort(MyUserPort)  ' wait for message from user port
    
GetNextMsg:
    
    MyMessage = GetMsg(MyUserPort) ' pointer to received Message structure

    IF MyMessage=0 THEN GOTO WaitAgain ' no more messages queued
  
    MyClass = PEEKL(MyMessage+Class)   

    ReplyMsg(MyMessage) ' tell inutition that got message
     
    IF MyClass = CLSWINDOW THEN
      CloseMe = TRUE
    ELSEIF MyClass = GADGETDOWN THEN
      LOCATE 16,15
      PRINT "String Gadget Selected"      
      LOCATE 20,20
      PRINT SPACE$(50)
    ELSEIF MyClass = GADGETUP THEN     ' for string-gadget means hit RETURN
      LOCATE 16,15
      PRINT "String Gadget Released"
      move MyRastPort,160,158
      StrLen = PEEKW(MyStringInfo+16&) ' 16 is offset to NumChars 
      text MyRastPort,MyBuffer,StrLen
    ELSE    
      GOTO GetNextMsg
    END IF

  WEND

  CloseWindow MyWindow
 
BailOut:
      
  FreeMem MyCoords,NumBytes
  FreeMem MyText,20&
  FreeMem MyBorder,16&
  FreeMem MyGadget,44&
  FreeMem MyStringInfo,36&

  LIBRARY CLOSE

  CHAIN MERGE "j:Structures4/ClearOverlay.h",9999,ALL,DELETE 9002-
  
ClearOverlayReturn:  
  
  CLEAR ,25000
  
  SYSTEM    
 
'**** Gadget Box Border Coordinate Data ****

  DATA  -7, -4
  DATA 200, -4
  DATA 200, 11
  DATA  -7, 11
  DATA  -7, -4


