home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / jËzyki_programowania / amigae / e_v3.2a / rkrmsrc / clipboard / clipdemo.e < prev   
Text File  |  1977-12-31  |  7KB  |  219 lines

  1. -> Clipdemo.e
  2. ->
  3. -> Demonstrate use of clipboard I/O.  Uses general functions provided in the
  4. -> module cbio.  Important note: when this code is run with older versions of
  5. -> the Amiga OS (i.e., before V36) a memory loss of 536 bytes will occur due
  6. -> to bugs in the clipboard device.
  7.  
  8. ->>> Header (globals)
  9. MODULE '*cbio',
  10.        'devices/clipboard',
  11.        'dos/dos',
  12.        'exec/ports',
  13.        'amigalib/ports',
  14.        'other/split'
  15.  
  16. ENUM ERR_NONE, ERR_ARGS, ERR_PORT
  17.  
  18. ENUM FORGETIT, READIT, WRITEIT, POSTIT
  19. ->>>
  20.  
  21. ->>> PROC main()
  22. PROC main() HANDLE
  23.   DEF todo, string, arglist:PTR TO LONG
  24.   todo:=FORGETIT
  25.   -> Very simple code to parse for arguments - will suffice for the sake of
  26.   -> this example
  27.   -> E-Note: use argSplit() to get arguments
  28.   IF NIL=(arglist:=argSplit()) THEN Raise(ERR_ARGS)
  29.   IF ListLen(arglist)>0
  30.     IF StrCmp(arglist[], '-r')
  31.       todo:=READIT
  32.     ELSEIF StrCmp(arglist[], '-w')
  33.       todo:=WRITEIT
  34.     ELSEIF StrCmp(arglist[], '-p')
  35.       todo:=POSTIT
  36.     ENDIF
  37.  
  38.     string:=NIL
  39.  
  40.     IF ListLen(arglist)>1 THEN string:=arglist[1]
  41.   ENDIF
  42.  
  43.   SELECT todo
  44.   CASE READIT
  45.     readClip()
  46.   CASE POSTIT
  47.     postClip(string)
  48.   CASE WRITEIT
  49.     writeClip(string)
  50.   DEFAULT
  51.     WriteF('\nPossible switches are:\n\n'+
  52. '-r            Read, and output contents of clipboard.\n\n'+
  53. '-w [string]   Write string to clipboard.\n\n'+
  54. '-p [string]   Write string to clipboard using the clipboard POST mechanism.\n\n'+
  55. '              The Post can be satisfied by reading data from\n'+
  56. '              the clipboard.  Note that the message may never\n'+
  57. '              be received if some other application posts, or\n')
  58.     WriteF(
  59. '              performs an immediate write to the clipboard.\n\n'+
  60. '              To run this test you must run two copies of this example.\n'+
  61. '              Use the -p switch with one to post data, and the -r switch\n'+
  62. '              with another to read the data.\n\n'+
  63. '              The process can be stopped by using the BREAK command,\n'+
  64. '              in which case this example checks the CLIP write ID\n'+
  65. '              to determine if it should write to the clipboard before\n'+
  66. '              exiting.\n\n')
  67.   ENDSELECT
  68. EXCEPT DO
  69.   SELECT exception
  70.   CASE ERR_ARGS;  WriteF('Error: could not split arguments\n')
  71.   ENDSELECT
  72. ENDPROC
  73. ->>>
  74.  
  75. ->>> PROC readClip()
  76. -> Read, and output FTXT in the clipboard.
  77. PROC readClip() HANDLE
  78.   DEF ior=NIL, buf:PTR TO cbbuf
  79.   -> Open clipboard.device unit 0
  80.   ior:=cbOpen(0)
  81.   -> Look for FTXT in clipboard
  82.   IF cbQueryFTXT(ior)
  83.     -> Obtain a copy of the contents of each CHRS chunk
  84.     WHILE buf:=cbReadCHRS(ior)
  85.       -> Process data
  86.       WriteF('\s\n', buf.mem)
  87.       -> Free buffer allocated by cbReadCHRS()
  88.       cbFreeBuf(buf)
  89.     ENDWHILE
  90.  
  91.     -> The next call is not really needed if you are sure you read to the end of
  92.     -> the clip.
  93.     cbReadDone(ior)
  94.   ELSE
  95.     WriteF('No FTXT in clipboard\n')
  96.   ENDIF
  97. EXCEPT DO
  98.   IF ior THEN cbClose(ior)
  99.   SELECT exception
  100.   CASE "CBOP";  WriteF('Error opening clipboard unit 0\n')
  101.   CASE "CBRD";  WriteF('Error reading from clipboard\n')
  102.   ENDSELECT
  103.   ReThrow()
  104. ENDPROC
  105. ->>>
  106.  
  107. ->>> PROC writeClip(string)
  108. -> Write a string to the clipboard
  109. PROC writeClip(string) HANDLE
  110.   DEF ior=NIL:PTR TO ioclipreq
  111.   IF string=NIL
  112.     WriteF('No string argument given\n')
  113.     RETURN
  114.   ENDIF
  115.  
  116.   -> Open clipboard.device unit 0
  117.   ior:=cbOpen(0)
  118.   cbWriteFTXT(ior, string)
  119. EXCEPT DO
  120.   IF ior THEN cbClose(ior)
  121.   SELECT exception
  122.   CASE "CBWR";  WriteF('Error writing to clipboard: error = \d\n', ior.error)
  123.   CASE "CBOP";  WriteF('Error opening clipboard.device\n')
  124.   ENDSELECT
  125.   ReThrow()
  126. ENDPROC
  127. ->>>
  128.  
  129. ->>> PROC postClip(string)
  130. -> Write a string to the clipboard using the POST mechanism
  131. ->
  132. -> The POST mechanism can be used by applications which want to defer writing
  133. -> text to the clipboard until another application needs it (by attempting to
  134. -> read it via CMD_READ).  However note that you still need to keep a copy of
  135. -> the data until you receive a SatisfyMsg from the clipboard.device, or your
  136. -> program exits.
  137. ->
  138. -> In most cases it is easier to write the data immediately.
  139. ->
  140. -> If your program receives the SatisfyMsg from the clipboard.device, you MUST
  141. -> write some data.  This is also how you reply to the message.
  142. ->
  143. -> If your program wants to exit before it has received the satisfymsg, you
  144. -> must check the clipid field at the time of the post against the current
  145. -> post ID which is obtained by sending the CBD_CURRENTWRITEID command.
  146. ->
  147. -> If the value in clipid (returned by CBD_CURRENTWRITEID) is greater than
  148. -> your post ID, it means that some other application has performed a post, or
  149. -> immediate write after your post, and that you're application will never
  150. -> receive the satisfymsg.
  151. ->
  152. -> If the value in clipid (returned by CBD_CURRENTWRITEID) is equal to your
  153. -> post ID, then you must write your data, and send CMD_UPDATE before exiting.
  154. PROC postClip(string) HANDLE
  155.   DEF satisfy=NIL:PTR TO mp, sm:PTR TO satisfymsg, ior=NIL:PTR TO ioclipreq,
  156.       mustwrite, postID
  157.   IF string=NIL
  158.     WriteF('No string argument given\n')
  159.     RETURN
  160.   ENDIF
  161.  
  162.   IF NIL=(satisfy:=createPort(0, 0)) THEN Raise(ERR_PORT)
  163.   -> Open clipboard.device unit 0
  164.   ior:=cbOpen(0)
  165.   mustwrite:=FALSE
  166.  
  167.   -> Notify clipboard we have data
  168.   ior.data:=satisfy
  169.   ior.clipid:=0
  170.   ior.command:=CBD_POST
  171.   DoIO(ior)
  172.  
  173.   postID:=ior.clipid
  174.  
  175.   WriteF('\nClipID = \d\n', postID)
  176.  
  177.   -> Wait for CTRL-C break, or message from clipboard
  178.   Wait(SIGBREAKF_CTRL_C OR Shl(1, satisfy.sigbit))
  179.  
  180.   -> See if we got a message, or a break
  181.   WriteF('Woke up\n')
  182.  
  183.   IF sm:=GetMsg(satisfy)
  184.     WriteF('Got a message from the clipboard\n\n')
  185.  
  186.     -> We got a message - we MUST write some data
  187.     mustwrite:=TRUE
  188.     -> E-Note: I think we should reply to the msg...
  189.     ReplyMsg(sm)
  190.   ELSE
  191.     -> Determine if we must write before exiting by checking to see if our
  192.     -> POST is still valid
  193.     ior.command:=CBD_CURRENTWRITEID
  194.     DoIO(ior)
  195.  
  196.     WriteF('CURRENTWRITEID = \d\n', ior.clipid)
  197.  
  198.     IF postID>=ior.clipid THEN mustwrite:=TRUE
  199.   ENDIF
  200.  
  201.   -> Write the string of text
  202.   IF mustwrite
  203.     cbWriteFTXT(ior, string)
  204.   ELSE
  205.     WriteF('No need to write to clipboard\n')
  206.   ENDIF
  207. EXCEPT DO
  208.   IF ior THEN cbClose(ior)
  209.   IF satisfy THEN deletePort(satisfy)
  210.   SELECT exception
  211.   CASE ERR_PORT;  WriteF('Error creating message port\n')
  212.   CASE "CBOP";    WriteF('Error opening clipboard.device\n')
  213.   CASE "CBWR";    WriteF('Error writing to clipboard\n')
  214.   ENDSELECT
  215.   ReThrow()
  216. ENDPROC
  217. ->>>
  218.  
  219.