winclip.pas 4.9 KB

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