winclip.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. {
  2. $Id$
  3. This file is part of the Free Pascal Integrated Development Environment
  4. Copyright (c) 1999 by Pierre Muller
  5. Connection with Windows Clipboard
  6. based on Ralph Brown Interrupt List
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {$i globdir.inc}
  14. unit WinClip;
  15. interface
  16. {$ifdef WinClipSupported}
  17. function WinClipboardSupported : boolean;
  18. function OpenWinClipboard : boolean;
  19. function EmptyWinClipboard : boolean;
  20. function GetTextWinClipboardSize : longint;
  21. function GetTextWinClipBoardData(var p : pchar;var l : longint) : boolean;
  22. function SetTextWinClipBoardData(p : pchar;l : longint) : boolean;
  23. {$endif WinClipSupported}
  24. implementation
  25. {$ifdef WinClipSupported}
  26. {$ifdef DOS}
  27. uses
  28. pmode,
  29. {$ifdef go32v2}
  30. {go32 sorry Gabor, but its still not compiling without that ! }
  31. {now it works. btw. you don't have to sorry - just to tell me... ;)) Gabor }
  32. {$endif go32v2}
  33. dos;
  34. {$endif DOS}
  35. {$ifdef win32}
  36. uses
  37. strings,windows;
  38. {$endif win32}
  39. {$ifdef DOS}
  40. function WinClipboardSupported : boolean;
  41. var
  42. r : registers;
  43. begin
  44. r.ax:=$1700;
  45. RealIntr($2F,r);
  46. WinClipboardSupported:=(r.ax<>$1700);
  47. end;
  48. function OpenWinClipboard : boolean;
  49. var
  50. r : Registers;
  51. begin
  52. r.ax:=$1701;
  53. RealIntr($2F,r);
  54. OpenWinClipboard:=(r.ax<>0);
  55. end;
  56. function EmptyWinClipboard : boolean;
  57. var
  58. r : Registers;
  59. begin
  60. r.ax:=$1702;
  61. RealIntr($2F,r);
  62. EmptyWinClipboard:=(r.ax<>0);
  63. end;
  64. function CloseWinClipboard : boolean;
  65. var
  66. r : Registers;
  67. begin
  68. r.ax:=$1708;
  69. RealIntr($2F,r);
  70. CloseWinClipboard:=(r.ax<>0);
  71. end;
  72. function InternGetDataSize : longint;
  73. var
  74. r : Registers;
  75. begin
  76. r.ax:=$1704;
  77. r.dx:=7 {OEM Text rather then 1 : Text };
  78. RealIntr($2F,r);
  79. InternGetDataSize:=(r.dx shl 16) + r.ax;
  80. end;
  81. {$endif DOS}
  82. {$ifdef win32}
  83. function WinClipboardSupported : boolean;
  84. begin
  85. WinClipboardSupported:=true;
  86. end;
  87. function OpenWinClipboard : boolean;
  88. begin
  89. OpenWinClipboard:=OpenClipboard(0);
  90. end;
  91. function EmptyWinClipboard : boolean;
  92. begin
  93. EmptyWinClipboard:=EmptyClipboard;
  94. end;
  95. function CloseWinClipboard : boolean;
  96. begin
  97. CloseWinClipboard:=CloseClipboard;
  98. end;
  99. function InternGetDataSize : longint;
  100. var HC : Handle;
  101. begin
  102. HC:=GetClipBoardData(CF_OEMTEXT);
  103. if HC<>0 then
  104. begin
  105. InternGetDataSize:=strlen(pchar(GlobalLock(HC)))+1;
  106. GlobalUnlock(HC);
  107. end
  108. else
  109. InternGetDataSize:=0;
  110. end;
  111. {$endif win32}
  112. function GetTextWinClipboardSize : longint;
  113. begin
  114. OpenWinClipboard;
  115. GetTextWinClipboardSize:=InternGetDataSize;
  116. CloseWinClipboard;
  117. end;
  118. function GetTextWinClipBoardData(var p : pchar;var l : longint) : boolean;
  119. var
  120. {$ifdef DOS}
  121. r : Registers;
  122. M : MemPtr;
  123. {$endif DOS}
  124. {$ifdef win32}
  125. h : HGlobal;
  126. pp : pchar;
  127. {$endif win32}
  128. begin
  129. p:=nil;
  130. GetTextWinClipBoardData:=False;
  131. if not OpenWinClipBoard then
  132. exit;
  133. {$ifdef DOS}
  134. l:=InternGetDataSize;
  135. if (l=0) or (l>65520) then
  136. begin
  137. l:=0;
  138. CloseWinClipBoard;
  139. exit;
  140. end;
  141. GetMem(p,l);
  142. GetDosMem(M,l);
  143. r.ax:=$1705;
  144. r.dx:=7{ OEM Text rather then 1 : Text };
  145. r.es:=M.DosSeg;
  146. r.bx:=M.DosOfs;
  147. RealIntr($2F,r);
  148. GetTextWinClipBoardData:=(r.ax<>0);
  149. {$endif DOS}
  150. {$ifdef win32}
  151. h:=GetClipboardData(CF_OEMTEXT);
  152. if h<>0 then
  153. begin
  154. pp:=pchar(GlobalLock(h));
  155. l:=strlen(pp)+1;
  156. getmem(p,l);
  157. move(pp^,p^,l);
  158. GlobalUnlock(h);
  159. end;
  160. GetTextWinClipBoardData:=h<>0;
  161. {$endif win32}
  162. CloseWinClipBoard;
  163. {$ifdef DOS}
  164. M.MoveDataFrom(l,P^);
  165. FreeDosMem(M);
  166. {$endif DOS}
  167. end;
  168. function SetTextWinClipBoardData(p : pchar;l : longint) : boolean;
  169. var
  170. {$ifdef DOS}
  171. r : Registers;
  172. M : MemPtr;
  173. {$endif DOS}
  174. {$ifdef win32}
  175. h : HGlobal;
  176. pp : pchar;
  177. res : boolean;
  178. {$endif win32}
  179. begin
  180. SetTextWinClipBoardData:=False;
  181. if (l=0) or (l>65520) then
  182. exit;
  183. if not OpenWinClipBoard then
  184. exit;
  185. EmptyWinClipBoard;
  186. {$ifdef DOS}
  187. GetDosMem(M,l+1);
  188. M.MoveDataTo(P^,l+1);
  189. r.ax:=$1703;
  190. r.dx:=7{ OEM Text rather then 1 : Text };
  191. r.es:=M.DosSeg;
  192. r.bx:=M.DosOfs;
  193. r.si:=l shr 16;
  194. r.cx:=l and $ffff;
  195. RealIntr($2F,r);
  196. SetTextWinClipBoardData:=(r.ax<>0);
  197. r.ax:=$1703;
  198. r.dx:=1{ Empty Text };
  199. r.es:=M.DosSeg;
  200. r.bx:=M.DosOfs;
  201. r.si:=0;
  202. r.cx:=0;
  203. RealIntr($2F,r);
  204. FreeDosMem(M);
  205. {$endif DOS}
  206. {$ifdef win32}
  207. h:=GlobalAlloc(GMEM_MOVEABLE or GMEM_DDESHARE,l+1);
  208. pp:=pchar(GlobalLock(h));
  209. move(p^,pp^,l+1);
  210. GlobalUnlock(h);
  211. res:=(SetClipboardData(CF_OEMTEXT,h)=h);
  212. h:=GlobalAlloc(GMEM_MOVEABLE or GMEM_DDESHARE,l+1);
  213. pp:=pchar(GlobalLock(h));
  214. OemToCharBuff(p,pp,l+1);
  215. SetClipboardData(CF_TEXT,h);
  216. GlobalUnlock(h);
  217. SetTextWinClipBoardData:=res;
  218. {$endif win32}
  219. CloseWinClipBoard;
  220. end;
  221. {$endif WinClipSupported}
  222. end.
  223. {
  224. $Log$
  225. Revision 1.3 2002-09-07 15:40:50 peter
  226. * old logs removed and tabs fixed
  227. }