CefDragData.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// Class used to represent drag data. The methods of this class may be called
  10. /// on any thread.
  11. /// </summary>
  12. public sealed unsafe partial class CefDragData
  13. {
  14. /// <summary>
  15. /// Create a new CefDragData object.
  16. /// </summary>
  17. public static CefDragData Create()
  18. {
  19. return CefDragData.FromNative(cef_drag_data_t.create());
  20. }
  21. /// <summary>
  22. /// Returns a copy of the current object.
  23. /// </summary>
  24. public CefDragData Clone()
  25. {
  26. return CefDragData.FromNative(cef_drag_data_t.clone(_self));
  27. }
  28. /// <summary>
  29. /// Returns true if this object is read-only.
  30. /// </summary>
  31. public bool IsReadOnly
  32. {
  33. get { return cef_drag_data_t.is_read_only(_self) != 0; }
  34. }
  35. /// <summary>
  36. /// Returns true if the drag data is a link.
  37. /// </summary>
  38. public bool IsLink
  39. {
  40. get { return cef_drag_data_t.is_link(_self) != 0; }
  41. }
  42. /// <summary>
  43. /// Returns true if the drag data is a text or html fragment.
  44. /// </summary>
  45. public bool IsFragment
  46. {
  47. get { return cef_drag_data_t.is_fragment(_self) != 0; }
  48. }
  49. /// <summary>
  50. /// Returns true if the drag data is a file.
  51. /// </summary>
  52. public bool IsFile
  53. {
  54. get { return cef_drag_data_t.is_file(_self) != 0; }
  55. }
  56. /// <summary>
  57. /// Return the link URL that is being dragged.
  58. /// </summary>
  59. public string LinkUrl
  60. {
  61. get
  62. {
  63. var n_result = cef_drag_data_t.get_link_url(_self);
  64. return cef_string_userfree.ToString(n_result);
  65. }
  66. }
  67. /// <summary>
  68. /// Return the title associated with the link being dragged.
  69. /// </summary>
  70. public string LinkTitle
  71. {
  72. get
  73. {
  74. var n_result = cef_drag_data_t.get_link_title(_self);
  75. return cef_string_userfree.ToString(n_result);
  76. }
  77. }
  78. /// <summary>
  79. /// Return the metadata, if any, associated with the link being dragged.
  80. /// </summary>
  81. public string LinkMetadata
  82. {
  83. get
  84. {
  85. var n_result = cef_drag_data_t.get_link_metadata(_self);
  86. return cef_string_userfree.ToString(n_result);
  87. }
  88. }
  89. /// <summary>
  90. /// Return the plain text fragment that is being dragged.
  91. /// </summary>
  92. public string FragmentText
  93. {
  94. get
  95. {
  96. var n_result = cef_drag_data_t.get_fragment_text(_self);
  97. return cef_string_userfree.ToString(n_result);
  98. }
  99. }
  100. /// <summary>
  101. /// Return the text/html fragment that is being dragged.
  102. /// </summary>
  103. public string FragmentHtml
  104. {
  105. get
  106. {
  107. var n_result = cef_drag_data_t.get_fragment_html(_self);
  108. return cef_string_userfree.ToString(n_result);
  109. }
  110. }
  111. /// <summary>
  112. /// Return the base URL that the fragment came from. This value is used for
  113. /// resolving relative URLs and may be empty.
  114. /// </summary>
  115. public string FragmentBaseUrl
  116. {
  117. get
  118. {
  119. var n_result = cef_drag_data_t.get_fragment_base_url(_self);
  120. return cef_string_userfree.ToString(n_result);
  121. }
  122. }
  123. /// <summary>
  124. /// Return the name of the file being dragged out of the browser window.
  125. /// </summary>
  126. public string FileName
  127. {
  128. get
  129. {
  130. var n_result = cef_drag_data_t.get_file_name(_self);
  131. return cef_string_userfree.ToString(n_result);
  132. }
  133. }
  134. /// <summary>
  135. /// Write the contents of the file being dragged out of the web view into
  136. /// |writer|. Returns the number of bytes sent to |writer|. If |writer| is
  137. /// NULL this method will return the size of the file contents in bytes.
  138. /// Call GetFileName() to get a suggested name for the file.
  139. /// </summary>
  140. public ulong GetFileContents(CefStreamWriter writer)
  141. {
  142. var n_writer = writer != null ? writer.ToNative() : null;
  143. return (ulong)cef_drag_data_t.get_file_contents(_self, n_writer);
  144. }
  145. /// <summary>
  146. /// Retrieve the list of file names that are being dragged into the browser
  147. /// window.
  148. /// </summary>
  149. public string[] GetFileNames()
  150. {
  151. cef_string_list* n_result = null;
  152. try
  153. {
  154. n_result = libcef.string_list_alloc();
  155. var success = cef_drag_data_t.get_file_names(_self, n_result) != 0;
  156. if (!success) return null;
  157. return cef_string_list.ToArray(n_result);
  158. }
  159. finally
  160. {
  161. if (n_result != null) libcef.string_list_free(n_result);
  162. }
  163. }
  164. /// <summary>
  165. /// Set the link URL that is being dragged.
  166. /// </summary>
  167. public void SetLinkURL(string url)
  168. {
  169. fixed (char* url_str = url)
  170. {
  171. var n_url = new cef_string_t(url_str, url != null ? url.Length : 0);
  172. cef_drag_data_t.set_link_url(_self, &n_url);
  173. }
  174. }
  175. /// <summary>
  176. /// Set the title associated with the link being dragged.
  177. /// </summary>
  178. public void SetLinkTitle(string title)
  179. {
  180. fixed (char* title_str = title)
  181. {
  182. var n_title = new cef_string_t(title_str, title != null ? title.Length : 0);
  183. cef_drag_data_t.set_link_title(_self, &n_title);
  184. }
  185. }
  186. /// <summary>
  187. /// Set the metadata associated with the link being dragged.
  188. /// </summary>
  189. public void SetLinkMetadata(string data)
  190. {
  191. fixed (char* data_str = data)
  192. {
  193. var n_data = new cef_string_t(data_str, data != null ? data.Length : 0);
  194. cef_drag_data_t.set_link_metadata(_self, &n_data);
  195. }
  196. }
  197. /// <summary>
  198. /// Set the plain text fragment that is being dragged.
  199. /// </summary>
  200. public void SetFragmentText(string text)
  201. {
  202. fixed (char* text_str = text)
  203. {
  204. var n_text = new cef_string_t(text_str, text != null ? text.Length : 0);
  205. cef_drag_data_t.set_fragment_text(_self, &n_text);
  206. }
  207. }
  208. /// <summary>
  209. /// Set the text/html fragment that is being dragged.
  210. /// </summary>
  211. public void SetFragmentHtml(string html)
  212. {
  213. fixed (char* html_str = html)
  214. {
  215. var n_html = new cef_string_t(html_str, html != null ? html.Length : 0);
  216. cef_drag_data_t.set_fragment_html(_self, &n_html);
  217. }
  218. }
  219. /// <summary>
  220. /// Set the base URL that the fragment came from.
  221. /// </summary>
  222. public void SetFragmentBaseURL(string baseUrl)
  223. {
  224. fixed (char* baseUrl_str = baseUrl)
  225. {
  226. var n_baseUrl = new cef_string_t(baseUrl_str, baseUrl != null ? baseUrl.Length : 0);
  227. cef_drag_data_t.set_fragment_base_url(_self, &n_baseUrl);
  228. }
  229. }
  230. /// <summary>
  231. /// Reset the file contents. You should do this before calling
  232. /// CefBrowserHost::DragTargetDragEnter as the web view does not allow us to
  233. /// drag in this kind of data.
  234. /// </summary>
  235. public void ResetFileContents()
  236. {
  237. cef_drag_data_t.reset_file_contents(_self);
  238. }
  239. /// <summary>
  240. /// Add a file that is being dragged into the webview.
  241. /// </summary>
  242. public void AddFile(string path, string displayName)
  243. {
  244. fixed (char* path_str = path)
  245. fixed (char* displayName_str = displayName)
  246. {
  247. var n_path = new cef_string_t(path_str, path != null ? path.Length : 0);
  248. var n_displayName = new cef_string_t(displayName_str, displayName != null ? displayName.Length : 0);
  249. cef_drag_data_t.add_file(_self, &n_path, &n_displayName);
  250. }
  251. }
  252. /// <summary>
  253. /// Get the image representation of drag data. May return NULL if no image
  254. /// representation is available.
  255. /// </summary>
  256. public CefImage GetImage()
  257. {
  258. var result = cef_drag_data_t.get_image(_self);
  259. return CefImage.FromNativeOrNull(result);
  260. }
  261. /// <summary>
  262. /// Get the image hotspot (drag start location relative to image dimensions).
  263. /// </summary>
  264. public CefPoint GetImageHotspot()
  265. {
  266. var result = cef_drag_data_t.get_image_hotspot(_self);
  267. return new CefPoint(result.x, result.y);
  268. }
  269. /// <summary>
  270. /// Returns true if an image representation of drag data is available.
  271. /// </summary>
  272. public bool HasImage
  273. {
  274. get
  275. {
  276. return cef_drag_data_t.has_image(_self) != 0;
  277. }
  278. }
  279. }
  280. }