winclip.pas 6.9 KB

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