summaryrefslogtreecommitdiff
path: root/rhtvision/classes/win32/win32clip.cc
blob: 0ceb52cb1a0573bb934c4fb5bd85f7a0d82ccaff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**[txh]********************************************************************

  Copyright (c) 2002 by Salvador E. Tropea (SET)
  Based on code contributed by Anatoli Soltan.

  Description:
  Win32 Clipboard routines.
  
***************************************************************************/

#include <tv/configtv.h>

#define Uses_string
#define Uses_TVOSClipboard
#include <tv.h>
#include <tv/win32/win32clip.h>

// I delay the check to generate as much dependencies as possible
#ifdef TVOS_Win32

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

// Strings for the errors
const char *TVWin32Clipboard::win32NameError[]=
{
 NULL,
 NULL
};

void TVWin32Clipboard::Init()
{
 TVOSClipboard::copy=copy;
 TVOSClipboard::paste=paste;
 TVOSClipboard::destroy=destroy;
 TVOSClipboard::available=1; // We have 1 clipboard
 TVOSClipboard::name="Windows";
 // We get the error from windows, so we just change the pointer
 TVOSClipboard::errors=1;
 TVOSClipboard::nameErrors=win32NameError;
}

void TVWin32Clipboard::getErrorString()
{
 if (win32NameError[1])
    LocalFree((LPVOID)win32NameError[1]);

 LPVOID lpMsgBuf;
 
 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
               NULL,GetLastError(),
               MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
               (LPTSTR)&lpMsgBuf,0,NULL);
 win32NameError[1]=(const char *)lpMsgBuf;
 TVOSClipboard::error=1;
}

void TVWin32Clipboard::destroy()
{
 if (win32NameError[1])
    LocalFree((LPVOID)win32NameError[1]);
}

char *TVWin32Clipboard::paste(int id, unsigned &len)
{
 if (id!=0) return NULL;

 char* p=NULL;
 HGLOBAL hMem;

 if (OpenClipboard(NULL))
   {
    hMem=GetClipboardData(CF_TEXT);
    if (hMem)
      {
       char *d=(char*)GlobalLock(hMem);
       if (d)
         {
          len=GlobalSize(hMem)-1;
          p=new char[len+1];
          memcpy(p,d,len);
          p[len]=0;
          // The returned length doesn't have to be the exact length of the string.
          // Reported by Warren H. Holt
          len=strlen(p);
          GlobalUnlock(hMem);
         }
       else
          getErrorString();
      }
    else
       getErrorString();
    CloseClipboard();
   }
 else
    getErrorString();

 return p;
}

int TVWin32Clipboard::copy(int id, const char *buffer, unsigned len)
{
 if (id!=0) return 0;
 if (!len || !buffer) return 1;

 HGLOBAL hMem=GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,len+1);
 if (!hMem)
   {
    getErrorString();
    return 0;
   }

 char *d=(char *)GlobalLock(hMem);
 if (!d)
   {
    getErrorString();
    GlobalFree(hMem);
    return 0;
   }
 memcpy(d,buffer,len);
 d[len]=0;
 GlobalUnlock(hMem);

 if (OpenClipboard(NULL))
   {
    EmptyClipboard();
    if (SetClipboardData(CF_TEXT,hMem))
      {
       CloseClipboard();
       // Windows now owns the memory, we doesn't have to release it.
       return 1;
      }
    getErrorString();
    CloseClipboard();
   }
 else
    getErrorString();
 GlobalFree(hMem);

 return 0;
}

#endif // TVOS_Win32