// ----
// ----    file: Clipboard_macosx.cpp
// ----  author: (C) 2005 Bastian Spiegel <bs@tkscript.de>
// ----    date: 23-Aug-2005
// ---- license: Distributed under terms of the Lesser General Public License (LGPL). 
// ----          See <http://www.gnu.org/licenses/licenses.html#LGPL> for further information.
// ----
// ----    note: 
// ----

#include <math.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#define YAC_PRINTF
#include <yac.h>

#include <Carbon/Carbon.h> 

#include "HAL.h"
extern YAC_Host *yac_host;

#include "Clipboard.h"


#define MAX_CLIPBOARD_SIZE 32768


// ----
// ---- Store the given object in the clipboard.
// ---- Currently, only String objects are supported which will be stored in ANSI 8Bit text format.
// ----
void SDL_SetClipboard(YAC_Object *_o) {
   if(YAC_BCHK(_o, YAC_CLID_STRING))
   {
      YAC_String *s=(YAC_String*)_o;
      if((s->length>1)&&(s->length<MAX_CLIPBOARD_SIZE))
      {
         ScrapRef scrapRef;
         //ClearCurrentScrap();
         GetCurrentScrap(&scrapRef);
         PutScrapFlavor(scrapRef, 'TEXT', 0, s->length, (char*)s->chars);
      }
   }
}

// ----
// ---- Query the current clipboard data, store in new object and return it.
// ---- Currently, only String object return values are supported.
// ----
void SDL_GetClipboard(YAC_Value *_r) {
   long length;
   
   YAC_String *s=YAC_New_String();
   _r->initString(s, 1);
   
   ScrapRef scrapRef; 
   GetCurrentScrap(&scrapRef); 
   GetScrapFlavorSize(scrapRef, 'TEXT', &length); 
   if(length>0)
   {
      s->yacArrayAlloc(length+1);
      GetScrapFlavorData(scrapRef, 'TEXT', &length, s->chars); 
      s->fixLength();
   }
}
