clipboard.bmx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. ' Copyright (c) 2018-2020 Bruce A Henderson
  2. '
  3. ' Permission is hereby granted, free of charge, to any person obtaining a copy
  4. ' of this software and associated documentation files (the "Software"), to deal
  5. ' in the Software without restriction, including without limitation the rights
  6. ' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. ' copies of the Software, and to permit persons to whom the Software is
  8. ' furnished to do so, subject to the following conditions:
  9. '
  10. ' The above copyright notice and this permission notice shall be included in
  11. ' all copies or substantial portions of the Software.
  12. '
  13. ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. ' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. ' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. ' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. ' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. ' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. ' THE SOFTWARE.
  20. '
  21. SuperStrict
  22. Rem
  23. bbdoc: Cross-platform clipboards.
  24. End Rem
  25. Module BRL.Clipboard
  26. ModuleInfo "Version: 1.02"
  27. ModuleInfo "License: MIT"
  28. ModuleInfo "Copyright: libclipboard - Copyright (C) 2016-2019 Jeremy Tan."
  29. ModuleInfo "Copyright: Wrapper - 2018-2020 Bruce A Henderson"
  30. ModuleInfo "History: 1.02"
  31. ModuleInfo "History: Fixed for Android."
  32. ModuleInfo "History: 1.01"
  33. ModuleInfo "History: Updated to latest libclipboard 1.0.efaa094"
  34. ModuleInfo "History: 1.00 Initial Release"
  35. ?win32
  36. ModuleInfo "CC_OPTS: -DLIBCLIPBOARD_BUILD_WIN32"
  37. ?linux And Not android
  38. ModuleInfo "CC_OPTS: -DLIBCLIPBOARD_BUILD_X11"
  39. ?macos
  40. ModuleInfo "CC_OPTS: -DLIBCLIPBOARD_BUILD_COCOA"
  41. ?haiku
  42. ModuleInfo "CC_OPTS: -DLIBCLIPBOARD_BUILD_HAIKU"
  43. ?
  44. '
  45. ' build notes :
  46. ' clipboard_cocoa.c renamed to clipboard_cocoa.m
  47. '
  48. ?Not android
  49. Import "common.bmx"
  50. ?android
  51. Import SDL.SDL
  52. ?
  53. Rem
  54. bbdoc: Options to be passed on instantiation.
  55. End Rem
  56. Type TClipboardOpts Abstract
  57. End Type
  58. Rem
  59. bbdoc: Win32 options
  60. End Rem
  61. Type TWin32ClipboardOpts Extends TClipboardOpts
  62. Rem
  63. bbdoc: Max number of retries to try to obtain clipboard lock.
  64. about: If @maxRetries is zero, the default value will be used. Specify a negative value for zero retries.
  65. End Rem
  66. Field maxRetries:Int = 5
  67. Rem
  68. bbdoc: Delay in ms between retries to obtain clipboard lock.
  69. about: If @retryDelay is zero, the default value will be used. Specify a negative value for no (zero) delay.
  70. End Rem
  71. Field retryDelay:Int = 5
  72. End Type
  73. Rem
  74. bbdoc: X11 options.
  75. End Rem
  76. Type TX11ClipboardOpts Extends TClipboardOpts
  77. Rem
  78. bbdoc: Max time (ms) to wait for action to complete.
  79. End Rem
  80. Field actionTimeout:Int = 1500
  81. Rem
  82. bbdoc: Transfer size, in bytes. Must be a multiple of 4.
  83. End Rem
  84. Field transferSize:Int = 1048576
  85. Rem
  86. bbdoc: The name of the X11 display (NULL for default - DISPLAY env. var.)
  87. End Rem
  88. Field displayName:String
  89. End Type
  90. ?Not android
  91. Rem
  92. bbdoc: A clipboard context.
  93. End Rem
  94. Type TClipboard
  95. Field clipboardPtr:Byte Ptr
  96. Rem
  97. bbdoc: Creates a new clipboard instance.
  98. returns: The clipboard instance, or Null on failure.
  99. End Rem
  100. Method Create:TClipboard(opts:TClipboardOpts = Null)
  101. If TWin32ClipboardOpts(opts) Then
  102. clipboardPtr = bmx_clipboard_new_win32(TWin32ClipboardOpts(opts).maxRetries, TWin32ClipboardOpts(opts).retryDelay)
  103. Else If TX11ClipboardOpts(opts) Then
  104. clipboardPtr = bmx_clipboard_new_x11(TX11ClipboardOpts(opts).actionTimeout, TX11ClipboardOpts(opts).transferSize, TX11ClipboardOpts(opts).displayName)
  105. Else
  106. clipboardPtr = bmx_clipboard_new()
  107. End If
  108. If Not clipboardPtr Then
  109. Return Null
  110. End If
  111. Return Self
  112. End Method
  113. Rem
  114. bbdoc: Clears the contents of the given clipboard.
  115. End Rem
  116. Method Clear(clipboardMode:Int = LCB_CLIPBOARD)
  117. bmx_clipboard_clear(clipboardPtr, clipboardMode)
  118. End Method
  119. Rem
  120. bbdoc: Determines if the clipboard content is currently owned.
  121. returns: #True if the clipboard data is owned by the provided instance.
  122. End Rem
  123. Method HasOwnership:Int(clipboardMode:Int = LCB_CLIPBOARD)
  124. Return bmx_clipboard_has_ownership(clipboardPtr, clipboardMode)
  125. End Method
  126. Rem
  127. bbdoc: Retrieves the text currently held on the clipboard.
  128. returns: A copy to the retrieved text.
  129. End Rem
  130. Method Text:String()
  131. Return bmx_clipboard_text(clipboardPtr)
  132. End Method
  133. Rem
  134. bbdoc: Retrieves the text currently held on the clipboard.
  135. about: @length returns the length of the retrieved data.
  136. returns: A copy to the retrieved text.
  137. End Rem
  138. Method TextEx:String(length:Int Var, clipboardMode:Int = LCB_CLIPBOARD)
  139. Return bmx_clipboard_text_ex(clipboardPtr, Varptr length, clipboardMode)
  140. End Method
  141. Rem
  142. bbdoc: Sets the text for the clipboard.
  143. returns: #True if the clipboard was set (#false on error).
  144. End Rem
  145. Method SetText:Int(src:String)
  146. Return bmx_clipboard_set_text(clipboardPtr, src)
  147. End Method
  148. Rem
  149. bbdoc: Sets the text for the clipboard.
  150. returns: #True if the clipboard was set (#false on error).
  151. End Rem
  152. Method SetTextEx:Int(src:String, clipboardMode:Int = LCB_CLIPBOARD)
  153. Return bmx_clipboard_set_text_ex(clipboardPtr, src, clipboardMode)
  154. End Method
  155. Rem
  156. bbdoc: Frees the clipboard.
  157. End Rem
  158. Method Free()
  159. If clipboardPtr Then
  160. bmx_clipboard_free(clipboardPtr)
  161. clipboardPtr = Null
  162. End If
  163. End Method
  164. Method Delete()
  165. Free()
  166. End Method
  167. End Type
  168. ?android
  169. Const LCB_CLIPBOARD:Int = 0
  170. Const LCB_PRIMARY:Int = 1
  171. Const LCB_SECONDARY:Int = 2
  172. Type TClipboard
  173. Method Create:TClipboard(opts:TClipboardOpts = Null)
  174. Return Self
  175. End Method
  176. Method Clear(clipboardMode:Int = LCB_CLIPBOARD)
  177. SDLSetClipboardText("")
  178. End Method
  179. Method HasOwnership:Int(clipboardMode:Int = LCB_CLIPBOARD)
  180. Return True
  181. End Method
  182. Method Text:String()
  183. Return SDLGetClipboardText()
  184. End Method
  185. Method TextEx:String(length:Int Var, clipboardMode:Int = LCB_CLIPBOARD)
  186. Local txt:String = SDLGetClipboardText()
  187. length = txt.length
  188. Return txt
  189. End Method
  190. Method SetText:Int(src:String)
  191. Return SDLSetClipboardText(src)
  192. End Method
  193. Method SetTextEx:Int(src:String, clipboardMode:Int = LCB_CLIPBOARD)
  194. Return SDLSetClipboardText(src)
  195. End Method
  196. End Type
  197. ?
  198. Rem
  199. bbdoc: Creates a new clipboard instance.
  200. returns: The clipboard instance, or Null on failure.
  201. End Rem
  202. Function CreateClipboard:TClipboard(opts:TClipboardOpts = Null)
  203. Return New TClipboard.Create( opts )
  204. End Function
  205. Rem
  206. bbdoc: Clears the contents of the given clipboard.
  207. End Rem
  208. Function ClearClipboard(clipboard:TClipboard, clipboardMode:Int = LCB_CLIPBOARD)
  209. clipboard.Clear( clipboardMode )
  210. End Function
  211. Rem
  212. bbdoc: Determines if the clipboard content is currently owned.
  213. returns: #True if the clipboard data is owned by the provided instance.
  214. End Rem
  215. Function ClipboardHasOwnership:Int(clipboard:TClipboard, clipboardMode:Int = LCB_CLIPBOARD)
  216. Return clipboard.HasOwnership(clipboardMode)
  217. End Function
  218. Rem
  219. bbdoc: Retrieves the text currently held on the clipboard.
  220. returns: A copy to the retrieved text.
  221. End Rem
  222. Function ClipboardText:String(clipboard:TClipboard)
  223. Return clipboard.Text()
  224. End Function
  225. Rem
  226. bbdoc: Retrieves the text currently held on the clipboard.
  227. about: @length returns the length of the retrieved data.
  228. returns: A copy to the retrieved text.
  229. End Rem
  230. Function ClipboardTextEx:String(clipboard:TClipboard, length:Int Var, clipboardMode:Int = LCB_CLIPBOARD)
  231. Return clipboard.TextEx(length, clipboardMode)
  232. End Function
  233. Rem
  234. bbdoc: Sets the text for the clipboard.
  235. returns: #True if the clipboard was set (#false on error).
  236. End Rem
  237. Function ClipboardSetText:Int(clipboard:TClipboard, src:String)
  238. Return clipboard.SetText(src)
  239. End Function
  240. Rem
  241. bbdoc: Sets the text for the clipboard.
  242. returns: #True if the clipboard was set (#false on error).
  243. End Rem
  244. Function ClipboardSetTextEx:Int(clipboard:TClipboard, src:String, clipboardMode:Int = LCB_CLIPBOARD)
  245. Return clipboard.SetTextEx(src, clipboardMode)
  246. End Function