changeset 4495:dbbfdb9ea716

Simplified clipboard API for sanity's sake. A complete clipboard implementation would support multiple formats that could be queried at runtime, events for when the clipboard contents changed, support for HTML, images, etc. We're not going that crazy, at least for now. :)
author Sam Lantinga <slouken@libsdl.org>
date Wed, 07 Jul 2010 23:54:03 -0700
parents 621dc91c7a04
children aea9e96d7973
files include/SDL_clipboard.h src/video/SDL_clipboard.c src/video/SDL_sysvideo.h src/video/SDL_video.c
diffstat 4 files changed, 38 insertions(+), 105 deletions(-) [+]
line wrap: on
line diff
--- a/include/SDL_clipboard.h	Wed Jul 07 23:26:07 2010 -0700
+++ b/include/SDL_clipboard.h	Wed Jul 07 23:54:03 2010 -0700
@@ -30,7 +30,6 @@
 #define _SDL_clipboard_h
 
 #include "SDL_stdinc.h"
-#include "SDL_surface.h"
 
 #include "begin_code.h"
 /* Set up for C function definitions, even when using C++ */
@@ -43,14 +42,14 @@
 /* Function prototypes */
 
 /**
- * \brief Put text into the clipboard
+ * \brief Put UTF-8 text into the clipboard
  *
  * \sa SDL_GetClipboardText()
  */
 extern DECLSPEC int SDLCALL SDL_SetClipboardText(const char *text);
 
 /**
- * \brief Get text from the clipboard, which must be freed with SDL_free()
+ * \brief Get UTF-8 text from the clipboard, which must be freed with SDL_free()
  *
  * \sa SDL_SetClipboardText()
  */
@@ -63,54 +62,6 @@
  */
 extern DECLSPEC SDL_bool SDLCALL SDL_HasClipboardText();
 
-/**
- * \brief Put an image into the clipboard
- *
- * \sa SDL_GetClipboardImage()
- */
-extern DECLSPEC int SDLCALL SDL_SetClipboardImage(SDL_Surface *image);
-
-/**
- * \brief Get image from the clipboard, which must be freed with SDL_FreeSurface()
- *
- * \sa SDL_SetClipboard()
- */
-extern DECLSPEC SDL_Surface * SDLCALL SDL_GetClipboardImage();
-
-/**
- * \brief Returns whether the clipboard has data in the specified format
- *
- * \sa SDL_GetClipboardImage()
- */
-extern DECLSPEC SDL_bool SDLCALL SDL_HasClipboardImage();
-
-/**
- * \brief Set the data in the clipboard in the specified format
- *
- * \sa SDL_GetClipboard()
- */
-extern DECLSPEC int SDLCALL SDL_SetClipboard(Uint32 format, void *data, size_t length);
-
-/**
- * \brief Get the data in the clipboard in the specified format, which must be
- *        freed with SDL_free()
- *
- * \sa SDL_SetClipboard()
- */
-extern DECLSPEC int SDLCALL SDL_GetClipboard(Uint32 format, void **data, size_t *length);
-
-/**
- * \brief Returns whether the clipboard has data in the specified format
- *
- * \sa SDL_GetClipboard()
- */
-extern DECLSPEC SDL_bool SDLCALL SDL_HasClipboardFormat(Uint32 format);
-
-/**
- * \brief Clear any data out of the clipboard, if possible.
- */
-extern DECLSPEC void SDLCALL SDL_ClearClipboard(void);
-
 
 /* Ends C function definitions when using C++ */
 #ifdef __cplusplus
--- a/src/video/SDL_clipboard.c	Wed Jul 07 23:26:07 2010 -0700
+++ b/src/video/SDL_clipboard.c	Wed Jul 07 23:54:03 2010 -0700
@@ -24,78 +24,50 @@
 #include "SDL_clipboard.h"
 #include "SDL_sysvideo.h"
 
-/* FOURCC values for text and image clipboard formats */
-#define TEXT_DATA  SDL_FOURCC('T', 'E', 'X', 'T')
-#define IMAGE_DATA SDL_FOURCC('B', 'M', 'P', ' ')
 
 int
 SDL_SetClipboardText(const char *text)
 {
-    return SDL_SetClipboard(TEXT_DATA, text, SDL_strlen(text)+1);
+    SDL_VideoDevice *_this = SDL_GetVideoDevice();
+
+    if (_this->SetClipboardText) {
+        return _this->SetClipboardText(_this, text);
+    } else {
+        _this->clipboard_text = SDL_strdup(text);
+        return 0;
+    }
 }
 
 char *
 SDL_GetClipboardText()
 {
-    void *data;
-    size_t length;
+    SDL_VideoDevice *_this = SDL_GetVideoDevice();
 
-    if (SDL_GetClipboard(TEXT_DATA, &data, &length) == 0) {
-        return SDL_static_cast(char*, data);
+    if (_this->GetClipboardText) {
+        return _this->GetClipboardText(_this);
     } else {
-        return NULL;
+        const char *text = _this->clipboard_text;
+        if (!text) {
+            text = "";
+        }
+        return SDL_strdup(text);
     }
 }
 
 SDL_bool
 SDL_HasClipboardText()
 {
-    return SDL_HasClipboardFormat(TEXT_DATA);
-}
-
-int
-SDL_SetClipboardImage(SDL_Surface *image)
-{
-    SDL_Unsupported();
-    return -1;
-}
-
-SDL_Surface *
-SDL_GetClipboardImage()
-{
-    SDL_Unsupported();
-    return NULL;
-}
-
-SDL_bool
-SDL_HasClipboardImage()
-{
-    return SDL_FALSE;
-}
+    SDL_VideoDevice *_this = SDL_GetVideoDevice();
 
-int
-SDL_SetClipboard(Uint32 format, void *data, size_t length)
-{
-    SDL_Unsupported();
-    return -1;
-}
-
-int
-SDL_GetClipboard(Uint32 format, void **data, size_t *length)
-{
-    SDL_Unsupported();
-    return -1;
-}
-
-SDL_bool
-SDL_HasClipboardFormat(Uint32 format)
-{
-    return SDL_FALSE;
-}
-
-void
-SDL_ClearClipboard(void)
-{
+    if (_this->HasClipboardText) {
+        return _this->HasClipboardText(_this);
+    } else {
+        if (_this->clipboard_text) {
+            return SDL_TRUE;
+        } else {
+            return SDL_FALSE;
+        }
+    }
 }
 
 /* vi: set ts=4 sw=4 expandtab: */
--- a/src/video/SDL_sysvideo.h	Wed Jul 07 23:26:07 2010 -0700
+++ b/src/video/SDL_sysvideo.h	Wed Jul 07 23:54:03 2010 -0700
@@ -303,6 +303,11 @@
     void (*StopTextInput) (_THIS);
     void (*SetTextInputRect) (_THIS, SDL_Rect *rect);
 
+    /* Clipboard */
+    int (*SetClipboardText) (_THIS, const char *text);
+    char * (*GetClipboardText) (_THIS);
+    SDL_bool (*HasClipboardText) (_THIS);
+
     /* * * */
     /* Data common to all drivers */
     SDL_bool suspend_screensaver;
@@ -312,6 +317,7 @@
     Uint8 window_magic;
     Uint8 texture_magic;
     Uint32 next_object_id;
+    char * clipboard_text;
 
     /* * * */
     /* Data used by the GL drivers */
--- a/src/video/SDL_video.c	Wed Jul 07 23:26:07 2010 -0700
+++ b/src/video/SDL_video.c	Wed Jul 07 23:54:03 2010 -0700
@@ -2833,6 +2833,10 @@
         SDL_free(_this->displays);
         _this->displays = NULL;
     }
+    if (_this->clipboard_text) {
+        SDL_free(_this->clipboard_text);
+        _this->clipboard_text = NULL;
+    }
     _this->free(_this);
     _this = NULL;
 }