Debian Codegraph @

src/af/xap/win/xap_Win32Clipboard.cpp

raw
1 /* AbiSource Application Framework
2 * Copyright (C) 1998 AbiSource, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA.
18 */
19
20 #include <windows.h>
21 #include <richedit.h>
22
23 #include "ut_debugmsg.h"
24 #include "ut_string.h"
25 #include "ut_assert.h"
26 #include "xap_Win32Clipboard.h"
27
28 //////////////////////////////////////////////////////////////////
29 //////////////////////////////////////////////////////////////////
30
31 XAP_Win32Clipboard::XAP_Win32Clipboard(void)
32 {
33 m_bOpen = false;
34 }
35
36 bool XAP_Win32Clipboard::openClipboard(HWND hWnd)
37 {
38 if (m_bOpen)
39 return false;
40
41 if (!OpenClipboard(hWnd))
42 return false;
43
44 #ifdef DEBUG
45 {
46 /*
47 UINT k = 0;
48 UINT f;
49 while ( (f=EnumClipboardFormats(k++)) )
50 UT_DEBUGMSG(("Clipboard contains format [%d]\n",f));
51 */
52 }
53 #endif /* DEBUG */
54
55 m_bOpen = true;
56 return true;
57 }
58
59 bool XAP_Win32Clipboard::closeClipboard(void)
60 {
61 m_bOpen = false;
62 return (CloseClipboard() != 0);
63 }
64
65 bool XAP_Win32Clipboard::clearClipboard(void)
66 {
67 return (EmptyClipboard() != 0);
68 }
69
70 //////////////////////////////////////////////////////////////////
71 //////////////////////////////////////////////////////////////////
72
73 bool XAP_Win32Clipboard::addData(const char * format, void* pData, UT_sint32 iNumBytes)
74 {
75 UINT iFormat = convertFormatString(format);
76 if (iFormat == 0)
77 return false;
78
79 HANDLE hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, iNumBytes);
80 if (!hData)
81 return false;
82
83 void* p = GlobalLock(hData);
84 if(!p) //GlobalLock can return NULL
85 {
86 UT_ASSERT_HARMLESS(p);
87 GlobalFree(hData);
88 return false;
89 }
90 memcpy(p, pData, iNumBytes);
91 GlobalUnlock(hData);
92
93 return (SetClipboardData(iFormat, hData) != NULL);
94
95 // TODO if SetClipboardData() fails, do we need to GlobalFree(hData) ??
96 }
97
98 // Caller should release using GlobalUnlock the memory handle returned
99 HANDLE XAP_Win32Clipboard::getHandleInFormat(const char * format)
100 {
101 UT_uintptr iFormat = convertFormatString(format);
102 if (iFormat == 0)
103 return NULL;
104
105 HANDLE hData = GetClipboardData(iFormat);
106 return (hData);
107 }
108
109 bool XAP_Win32Clipboard::hasFormat(const char * format)
110 {
111 UINT iFormat = convertFormatString(format);
112 if (iFormat == 0)
113 return false;
114
115 return (IsClipboardFormatAvailable(iFormat) != FALSE);
116 }
117
118 //////////////////////////////////////////////////////////////////
119 //////////////////////////////////////////////////////////////////
120
121 UT_uintptr XAP_Win32Clipboard::convertFormatString(const char * format) const
122 {
123 // convert from named type (like "text-8bit") to a registered
124 // clipboard format (either a MSFT-defined CF_ symbol or something
125 // that we registered).
126
127 int kLimit = m_vecFormat.getItemCount();
128 int k;
129
130 for (k=0; k<kLimit; k++)
131 if (g_ascii_strcasecmp(format,(const char *)m_vecFormat.getNthItem(k)) == 0)
132 return (UT_uintptr)m_vecCF.getNthItem(k);
133
134 return 0;
135 }
136
137 const char * XAP_Win32Clipboard::convertToFormatString(UT_uintptr fmt) const
138 {
139 // convert from a CF_ symbol or something that we registered
140 // into a named format type (like "text-8bit").
141
142 int kLimit = m_vecFormat.getItemCount();
143 int k;
144
145 for (k=0; k<kLimit; k++)
146 if (fmt == (UT_uintptr)m_vecCF.getNthItem(k))
147 return (const char *)m_vecFormat.getNthItem(k);
148
149 return NULL;
150 }