SDL_cocoaclipboard.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_COCOA
  20. #include "SDL_cocoavideo.h"
  21. #include "../../events/SDL_clipboardevents_c.h"
  22. int
  23. Cocoa_SetClipboardText(_THIS, const char *text)
  24. { @autoreleasepool
  25. {
  26. SDL_VideoData *data = (__bridge SDL_VideoData *) _this->driverdata;
  27. NSPasteboard *pasteboard;
  28. NSString *format = NSPasteboardTypeString;
  29. NSString *nsstr = [NSString stringWithUTF8String:text];
  30. if (nsstr == nil) {
  31. return SDL_SetError("Couldn't create NSString; is your string data in UTF-8 format?");
  32. }
  33. pasteboard = [NSPasteboard generalPasteboard];
  34. data.clipboard_count = [pasteboard declareTypes:[NSArray arrayWithObject:format] owner:nil];
  35. [pasteboard setString:nsstr forType:format];
  36. return 0;
  37. }}
  38. char *
  39. Cocoa_GetClipboardText(_THIS)
  40. { @autoreleasepool
  41. {
  42. NSPasteboard *pasteboard;
  43. NSString *format = NSPasteboardTypeString;
  44. NSString *available;
  45. char *text;
  46. pasteboard = [NSPasteboard generalPasteboard];
  47. available = [pasteboard availableTypeFromArray:[NSArray arrayWithObject:format]];
  48. if ([available isEqualToString:format]) {
  49. NSString* string;
  50. const char *utf8;
  51. string = [pasteboard stringForType:format];
  52. if (string == nil) {
  53. utf8 = "";
  54. } else {
  55. utf8 = [string UTF8String];
  56. }
  57. text = SDL_strdup(utf8 ? utf8 : "");
  58. } else {
  59. text = SDL_strdup("");
  60. }
  61. return text;
  62. }}
  63. SDL_bool
  64. Cocoa_HasClipboardText(_THIS)
  65. {
  66. SDL_bool result = SDL_FALSE;
  67. char *text = Cocoa_GetClipboardText(_this);
  68. if (text) {
  69. result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
  70. SDL_free(text);
  71. }
  72. return result;
  73. }
  74. void
  75. Cocoa_CheckClipboardUpdate(SDL_VideoData * data)
  76. { @autoreleasepool
  77. {
  78. NSPasteboard *pasteboard;
  79. NSInteger count;
  80. pasteboard = [NSPasteboard generalPasteboard];
  81. count = [pasteboard changeCount];
  82. if (count != data.clipboard_count) {
  83. if (data.clipboard_count) {
  84. SDL_SendClipboardUpdate();
  85. }
  86. data.clipboard_count = count;
  87. }
  88. }}
  89. #endif /* SDL_VIDEO_DRIVER_COCOA */
  90. /* vi: set ts=4 sw=4 expandtab: */