winclip.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 HASAMIGA}
  39. uses
  40. clipboard,cliputils;
  41. {$endif}
  42. {$ifdef DOS}
  43. function WinClipboardSupported : boolean;
  44. var
  45. r : registers;
  46. begin
  47. r.ax:=$1700;
  48. RealIntr($2F,r);
  49. WinClipboardSupported:=(r.ax<>$1700);
  50. end;
  51. function OpenWinClipboard : boolean;
  52. var
  53. r : Registers;
  54. begin
  55. r.ax:=$1701;
  56. RealIntr($2F,r);
  57. OpenWinClipboard:=(r.ax<>0);
  58. end;
  59. function EmptyWinClipboard : boolean;
  60. var
  61. r : Registers;
  62. begin
  63. r.ax:=$1702;
  64. RealIntr($2F,r);
  65. EmptyWinClipboard:=(r.ax<>0);
  66. end;
  67. function CloseWinClipboard : boolean;
  68. var
  69. r : Registers;
  70. begin
  71. r.ax:=$1708;
  72. RealIntr($2F,r);
  73. CloseWinClipboard:=(r.ax<>0);
  74. end;
  75. function InternGetDataSize : longint;
  76. var
  77. r : Registers;
  78. begin
  79. r.ax:=$1704;
  80. r.dx:=7 {OEM Text rather then 1 : Text };
  81. RealIntr($2F,r);
  82. InternGetDataSize:=(r.dx shl 16) + r.ax;
  83. end;
  84. {$endif DOS}
  85. {$ifdef Windows}
  86. function WinClipboardSupported : boolean;
  87. begin
  88. WinClipboardSupported:=true;
  89. end;
  90. function OpenWinClipboard : boolean;
  91. begin
  92. OpenWinClipboard:=OpenClipboard(0);
  93. end;
  94. function EmptyWinClipboard : boolean;
  95. begin
  96. EmptyWinClipboard:=EmptyClipboard;
  97. end;
  98. function CloseWinClipboard : boolean;
  99. begin
  100. CloseWinClipboard:=CloseClipboard;
  101. end;
  102. function InternGetDataSize : longint;
  103. var HC : Handle;
  104. begin
  105. HC:=GetClipBoardData(CF_OEMTEXT);
  106. if HC<>0 then
  107. begin
  108. InternGetDataSize:=strlen(pchar(GlobalLock(HC)))+1;
  109. GlobalUnlock(HC);
  110. end
  111. else
  112. InternGetDataSize:=0;
  113. end;
  114. {$endif Windows}
  115. {$ifdef HASAMIGA}
  116. function WinClipboardSupported: Boolean;
  117. begin
  118. WinClipboardSupported := True;
  119. end;
  120. function OpenWinClipboard: boolean;
  121. begin
  122. OpenWinClipboard := True;
  123. end;
  124. function EmptyWinClipboard: boolean;
  125. begin
  126. EmptyWinClipboard := GetTextFromClip(PRIMARY_CLIP) = '';
  127. end;
  128. function CloseWinClipboard : boolean;
  129. begin
  130. CloseWinClipboard:= True;
  131. end;
  132. function InternGetDataSize: LongInt;
  133. var
  134. Text: string;
  135. begin
  136. Text := GetTextFromClip(PRIMARY_CLIP);
  137. InternGetDataSize := Length(Text);
  138. end;
  139. {$endif HASAMIGA}
  140. function GetTextWinClipboardSize : longint;
  141. begin
  142. OpenWinClipboard;
  143. GetTextWinClipboardSize:=InternGetDataSize;
  144. CloseWinClipboard;
  145. end;
  146. function GetTextWinClipBoardData(var p : pchar;var l : longint) : boolean;
  147. var
  148. {$ifdef DOS}
  149. r : Registers;
  150. M : MemPtr;
  151. {$endif DOS}
  152. {$ifdef Windows}
  153. h : HGlobal;
  154. pp : pchar;
  155. {$endif Windows}
  156. {$ifdef HASAMIGA}
  157. Text: AnsiString;
  158. pp: PChar;
  159. {$endif HASAMIGA}
  160. begin
  161. p:=nil;
  162. GetTextWinClipBoardData:=False;
  163. if not OpenWinClipBoard then
  164. exit;
  165. {$ifdef DOS}
  166. l:=InternGetDataSize;
  167. if (l=0) or (l>65520) then
  168. begin
  169. l:=0;
  170. CloseWinClipBoard;
  171. exit;
  172. end;
  173. GetMem(p,l);
  174. GetDosMem(M,l);
  175. r.ax:=$1705;
  176. r.dx:=7{ OEM Text rather then 1 : Text };
  177. r.es:=M.DosSeg;
  178. r.bx:=M.DosOfs;
  179. RealIntr($2F,r);
  180. GetTextWinClipBoardData:=(r.ax<>0);
  181. {$endif DOS}
  182. {$ifdef Windows}
  183. h:=GetClipboardData(CF_OEMTEXT);
  184. if h<>0 then
  185. begin
  186. pp:=pchar(GlobalLock(h));
  187. l:=strlen(pp)+1;
  188. getmem(p,l);
  189. move(pp^,p^,l);
  190. GlobalUnlock(h);
  191. end;
  192. GetTextWinClipBoardData:=h<>0;
  193. {$endif Windows}
  194. {$ifdef HASAMIGA}
  195. Text := GetTextFromClip(0) + #0;
  196. PP := @Text[1];
  197. l := Length(Text);
  198. GetMem(p,l);
  199. Move(pp^,p^,l);
  200. GetTextWinClipBoardData := True;
  201. {$endif HASAMIGA}
  202. CloseWinClipBoard;
  203. {$ifdef DOS}
  204. M.MoveDataFrom(l,P^);
  205. FreeDosMem(M);
  206. {$endif DOS}
  207. end;
  208. function SetTextWinClipBoardData(p : pchar;l : longint) : boolean;
  209. var
  210. {$ifdef DOS}
  211. r : Registers;
  212. M : MemPtr;
  213. {$endif DOS}
  214. {$ifdef Windows}
  215. h : HGlobal;
  216. pp : pchar;
  217. res : boolean;
  218. {$endif Windows}
  219. {$ifdef HASAMIGA}
  220. pp: PChar;
  221. Test: AnsiString;
  222. {$endif HASAMIGA}
  223. begin
  224. SetTextWinClipBoardData:=False;
  225. if (l=0) or (l>65520) then
  226. exit;
  227. if not OpenWinClipBoard then
  228. exit;
  229. EmptyWinClipBoard;
  230. {$ifdef DOS}
  231. GetDosMem(M,l+1);
  232. M.MoveDataTo(P^,l+1);
  233. r.ax:=$1703;
  234. r.dx:=7{ OEM Text rather then 1 : Text };
  235. r.es:=M.DosSeg;
  236. r.bx:=M.DosOfs;
  237. r.si:=l shr 16;
  238. r.cx:=l and $ffff;
  239. RealIntr($2F,r);
  240. SetTextWinClipBoardData:=(r.ax<>0);
  241. r.ax:=$1703;
  242. r.dx:=1{ Empty Text };
  243. r.es:=M.DosSeg;
  244. r.bx:=M.DosOfs;
  245. r.si:=0;
  246. r.cx:=0;
  247. RealIntr($2F,r);
  248. FreeDosMem(M);
  249. {$endif DOS}
  250. {$ifdef Windows}
  251. h:=GlobalAlloc(GMEM_MOVEABLE or GMEM_DDESHARE,l+1);
  252. pp:=pchar(GlobalLock(h));
  253. move(p^,pp^,l+1);
  254. GlobalUnlock(h);
  255. res:=(SetClipboardData(CF_OEMTEXT,h)=h);
  256. h:=GlobalAlloc(GMEM_MOVEABLE or GMEM_DDESHARE,l+1);
  257. pp:=pchar(GlobalLock(h));
  258. OemToCharBuffA(p,pp,l+1);
  259. SetClipboardData(CF_TEXT,h);
  260. GlobalUnlock(h);
  261. SetTextWinClipBoardData:=res;
  262. {$endif Windows}
  263. {$ifdef HASAMIGA}
  264. PutTextToClip(0, AnsiString(p));
  265. {$endif HASAMIGA}
  266. CloseWinClipBoard;
  267. end;
  268. {$endif WinClipSupported}
  269. end.