winclip.pas 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. unit WinClip;
  14. interface
  15. {$i globdir.inc}
  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 go32v2}
  27. uses
  28. strings,go32;
  29. {$endif go32v2}
  30. {$ifdef win32}
  31. uses
  32. strings,windows;
  33. {$endif win32}
  34. {$ifdef go32v2}
  35. function WinClipboardSupported : boolean;
  36. var
  37. r : Registers;
  38. begin
  39. r.ax:=$1700;
  40. RealIntr($2F,r);
  41. WinClipboardSupported:=(r.ax<>$1700);
  42. end;
  43. function OpenWinClipboard : boolean;
  44. var
  45. r : Registers;
  46. begin
  47. r.ax:=$1701;
  48. RealIntr($2F,r);
  49. OpenWinClipboard:=(r.ax<>0);
  50. end;
  51. function EmptyWinClipboard : boolean;
  52. var
  53. r : Registers;
  54. begin
  55. r.ax:=$1702;
  56. RealIntr($2F,r);
  57. EmptyWinClipboard:=(r.ax<>0);
  58. end;
  59. function CloseWinClipboard : boolean;
  60. var
  61. r : Registers;
  62. begin
  63. r.ax:=$1708;
  64. RealIntr($2F,r);
  65. CloseWinClipboard:=(r.ax<>0);
  66. end;
  67. function InternGetDataSize : longint;
  68. var
  69. r : Registers;
  70. begin
  71. r.ax:=$1704;
  72. r.dx:=7 {OEM Text rather then 1 : Text };
  73. RealIntr($2F,r);
  74. InternGetDataSize:=(r.dx shl 16) + r.ax;
  75. end;
  76. {$endif go32v2}
  77. {$ifdef win32}
  78. function WinClipboardSupported : boolean;
  79. begin
  80. WinClipboardSupported:=true;
  81. end;
  82. function OpenWinClipboard : boolean;
  83. begin
  84. OpenWinClipboard:=OpenClipboard(0);
  85. end;
  86. function EmptyWinClipboard : boolean;
  87. begin
  88. EmptyWinClipboard:=EmptyClipboard;
  89. end;
  90. function CloseWinClipboard : boolean;
  91. begin
  92. CloseWinClipboard:=CloseClipboard;
  93. end;
  94. function InternGetDataSize : longint;
  95. var HC : Handle;
  96. begin
  97. HC:=GetClipBoardData(CF_OEMTEXT);
  98. if HC<>0 then
  99. begin
  100. InternGetDataSize:=strlen(pchar(GlobalLock(HC)));
  101. GlobalUnlock(HC);
  102. end
  103. else
  104. InternGetDataSize:=0;
  105. end;
  106. {$endif win32}
  107. function GetTextWinClipboardSize : longint;
  108. begin
  109. OpenWinClipboard;
  110. GetTextWinClipboardSize:=InternGetDataSize;
  111. CloseWinClipboard;
  112. end;
  113. function GetTextWinClipBoardData(var p : pchar;var l : longint) : boolean;
  114. var
  115. {$ifdef go32v2}
  116. r : Registers;
  117. tb_all : longint;
  118. tb_seg,tb_ofs,tb_sel : word;
  119. {$endif go32v2}
  120. {$ifdef win32}
  121. h : HGlobal;
  122. pp : pchar;
  123. {$endif win32}
  124. begin
  125. p:=nil;
  126. GetTextWinClipBoardData:=False;
  127. if not OpenWinClipBoard then
  128. exit;
  129. {$ifdef go32v2}
  130. l:=InternGetDataSize;
  131. if (l=0) or (l>100000) then
  132. begin
  133. l:=0;
  134. CloseWinClipBoard;
  135. exit;
  136. end;
  137. GetMem(p,l);
  138. if l>tb_size then
  139. begin
  140. tb_all:=global_dos_alloc(l);
  141. { zero means allocation failure }
  142. if tb_all=0 then
  143. begin
  144. FreeMem(p,l);
  145. p:=nil;
  146. l:=0;
  147. CloseWinClipBoard;
  148. exit;
  149. end;
  150. tb_seg:=tb_all shr 16;
  151. tb_sel:=tb_all and $ffff;
  152. end
  153. else
  154. begin
  155. tb_seg:=tb_segment;
  156. tb_ofs:=tb_offset;
  157. tb_sel:=0;
  158. end;
  159. r.ax:=$1705;
  160. r.dx:=7{ OEM Text rather then 1 : Text };
  161. r.es:=tb_seg;
  162. r.bx:=tb_ofs;
  163. RealIntr($2F,r);
  164. GetTextWinClipBoardData:=(r.ax<>0);
  165. {$endif go32v2}
  166. {$ifdef win32}
  167. h:=GetClipboardData(CF_OEMTEXT);
  168. if h<>0 then
  169. begin
  170. pp:=pchar(GlobalLock(h));
  171. l:=strlen(pp)+1;
  172. getmem(p,l);
  173. move(pp^,p^,l);
  174. GlobalUnlock(h);
  175. end;
  176. GetTextWinClipBoardData:=h<>0;
  177. {$endif win32}
  178. CloseWinClipBoard;
  179. {$ifdef go32v2}
  180. DosMemGet(tb_seg,tb_ofs,p^,l);
  181. if tb_sel<>0 then
  182. global_dos_free(tb_sel);
  183. {$endif go32v2}
  184. end;
  185. function SetTextWinClipBoardData(p : pchar;l : longint) : boolean;
  186. var
  187. {$ifdef go32v2}
  188. r : Registers;
  189. tb_all : longint;
  190. tb_seg,tb_ofs,tb_sel : word;
  191. {$endif go32v2}
  192. {$ifdef win32}
  193. h : HGlobal;
  194. pp : pchar;
  195. {$endif win32}
  196. begin
  197. SetTextWinClipBoardData:=False;
  198. if (l=0) or (l>100000) then
  199. exit;
  200. if not OpenWinClipBoard then
  201. exit;
  202. EmptyWinClipBoard;
  203. {$ifdef go32v2}
  204. if l>tb_size then
  205. begin
  206. tb_all:=global_dos_alloc(l);
  207. { zero means allocation failure }
  208. if tb_all=0 then
  209. begin
  210. CloseWinClipBoard;
  211. exit;
  212. end;
  213. tb_seg:=tb_all shr 16;
  214. tb_sel:=tb_all and $ffff;
  215. end
  216. else
  217. begin
  218. tb_seg:=tb_segment;
  219. tb_ofs:=tb_offset;
  220. tb_sel:=0;
  221. end;
  222. DosMemPut(tb_seg,tb_ofs,p^,l);
  223. r.ax:=$1703;
  224. r.dx:=7{ OEM Text rather then 1 : Text };
  225. r.es:=tb_seg;
  226. r.bx:=tb_ofs;
  227. r.si:=l shr 16;
  228. r.cx:=l and $ffff;
  229. RealIntr($2F,r);
  230. SetTextWinClipBoardData:=(r.ax<>0);
  231. if tb_sel<>0 then
  232. global_dos_free(tb_sel);
  233. {$endif go32v2}
  234. {$ifdef win32}
  235. h:=GlobalAlloc(GMEM_MOVEABLE or GMEM_DDESHARE,l);
  236. pp:=pchar(GlobalLock(h));
  237. move(p^,pp^,l);
  238. GlobalUnlock(h);
  239. SetTextWinClipBoardData:=(SetClipboardData(CF_OEMTEXT,h)=h);
  240. {$endif win32}
  241. CloseWinClipBoard;
  242. end;
  243. {$endif WinClipSupported}
  244. end.
  245. {
  246. $Log$
  247. Revision 1.4 1999-11-05 13:46:26 pierre
  248. * Use CF_OEMTEXT under win32 and dx=7 under go32v2 to obtain
  249. OEM to ANSI conversion
  250. * GetClipboardDataSize for Win32
  251. Revision 1.3 1999/10/14 14:22:23 florian
  252. * if no ini file is found the ide uses some useful defaults
  253. }