]> git.saurik.com Git - wxWidgets.git/blame - src/tiff/tif_win32.c
capture mouse to be notified when it exists the popup rect (bug 1372228)
[wxWidgets.git] / src / tiff / tif_win32.c
CommitLineData
b47c832e
RR
1/* $Header$ */
2
3/*
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26
27/*
28 * TIFF Library Win32-specific Routines. Adapted from tif_unix.c 4/5/95 by
29 * Scott Wagner (wagner@itek.com), Itek Graphix, Rochester, NY USA
30 */
31#include <windows.h>
32#include "tiffiop.h"
33
34static tsize_t
35_tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
36{
37 DWORD dwSizeRead;
38 if (!ReadFile(fd, buf, size, &dwSizeRead, NULL))
39 return(0);
40 return ((tsize_t) dwSizeRead);
41}
42
43static tsize_t
44_tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
45{
46 DWORD dwSizeWritten;
47 if (!WriteFile(fd, buf, size, &dwSizeWritten, NULL))
48 return(0);
49 return ((tsize_t) dwSizeWritten);
50}
51
52static toff_t
53_tiffSeekProc(thandle_t fd, toff_t off, int whence)
54{
00cb87b4
VZ
55 DWORD dwMoveMethod, dwMoveHigh;
56
57 /* we use this as a special code, so avoid accepting it */
58 if( off == 0xFFFFFFFF )
59 return 0xFFFFFFFF;
60
b47c832e
RR
61 switch(whence)
62 {
00cb87b4 63 case SEEK_SET:
b47c832e
RR
64 dwMoveMethod = FILE_BEGIN;
65 break;
00cb87b4 66 case SEEK_CUR:
b47c832e
RR
67 dwMoveMethod = FILE_CURRENT;
68 break;
00cb87b4 69 case SEEK_END:
b47c832e
RR
70 dwMoveMethod = FILE_END;
71 break;
72 default:
73 dwMoveMethod = FILE_BEGIN;
74 break;
75 }
00cb87b4
VZ
76 dwMoveHigh = 0;
77 return ((toff_t)SetFilePointer(fd, (LONG) off, (PLONG)&dwMoveHigh,
78 dwMoveMethod));
b47c832e
RR
79}
80
81static int
82_tiffCloseProc(thandle_t fd)
83{
84 return (CloseHandle(fd) ? 0 : -1);
85}
86
87static toff_t
88_tiffSizeProc(thandle_t fd)
89{
90 return ((toff_t)GetFileSize(fd, NULL));
91}
92
00cb87b4
VZ
93#ifdef __BORLANDC__
94#pragma argsused
95#endif
b47c832e
RR
96static int
97_tiffDummyMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
98{
99 return (0);
100}
101
102/*
103 * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
104 *
105 * Windows uses both a handle and a pointer for file mapping,
106 * but according to the SDK documentation and Richter's book
107 * "Advanced Windows Programming" it is safe to free the handle
108 * after obtaining the file mapping pointer
109 *
110 * This removes a nasty OS dependency and cures a problem
111 * with Visual C++ 5.0
112 */
113static int
114_tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
115{
116 toff_t size;
117 HANDLE hMapFile;
118
00cb87b4 119 if ((size = _tiffSizeProc(fd)) == 0xFFFFFFFF)
b47c832e
RR
120 return (0);
121 hMapFile = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, size, NULL);
122 if (hMapFile == NULL)
123 return (0);
124 *pbase = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
125 CloseHandle(hMapFile);
126 if (*pbase == NULL)
127 return (0);
128 *psize = size;
129 return(1);
130}
131
00cb87b4
VZ
132#ifdef __BORLANDC__
133#pragma argsused
134#endif
b47c832e
RR
135static void
136_tiffDummyUnmapProc(thandle_t fd, tdata_t base, toff_t size)
137{
138}
139
140static void
141_tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
142{
143 UnmapViewOfFile(base);
144}
145
146/*
147 * Open a TIFF file descriptor for read/writing.
148 * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
149 * string, which forces the file to be opened unmapped.
150 */
151TIFF*
152TIFFFdOpen(int ifd, const char* name, const char* mode)
153{
154 TIFF* tif;
00cb87b4 155 BOOL fSuppressMap = (mode[1] == 'u' || (mode[1]!=0 && mode[2] == 'u'));
b47c832e
RR
156
157 tif = TIFFClientOpen(name, mode,
158 (thandle_t)ifd,
159 _tiffReadProc, _tiffWriteProc,
160 _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
161 fSuppressMap ? _tiffDummyMapProc : _tiffMapProc,
162 fSuppressMap ? _tiffDummyUnmapProc : _tiffUnmapProc);
163 if (tif)
164 tif->tif_fd = ifd;
165 return (tif);
166}
167
168/*
169 * Open a TIFF file for read/writing.
170 */
171TIFF*
172TIFFOpen(const char* name, const char* mode)
173{
174 static const char module[] = "TIFFOpen";
175 thandle_t fd;
176 int m;
177 DWORD dwMode;
178
179 m = _TIFFgetMode(mode, module);
180
181 switch(m)
182 {
183 case O_RDONLY:
184 dwMode = OPEN_EXISTING;
185 break;
186 case O_RDWR:
187 dwMode = OPEN_ALWAYS;
188 break;
189 case O_RDWR|O_CREAT:
190 dwMode = OPEN_ALWAYS;
191 break;
192 case O_RDWR|O_TRUNC:
193 dwMode = CREATE_ALWAYS;
194 break;
195 case O_RDWR|O_CREAT|O_TRUNC:
196 dwMode = CREATE_ALWAYS;
197 break;
198 default:
199 return ((TIFF*)0);
200 }
201 fd = (thandle_t)CreateFile(name, (m == O_RDONLY) ? GENERIC_READ :
202 (GENERIC_READ | GENERIC_WRITE), FILE_SHARE_READ, NULL, dwMode,
203 (m == O_RDONLY) ? FILE_ATTRIBUTE_READONLY : FILE_ATTRIBUTE_NORMAL, NULL);
204 if (fd == INVALID_HANDLE_VALUE) {
205 TIFFError(module, "%s: Cannot open", name);
206 return ((TIFF *)0);
207 }
208 return (TIFFFdOpen((int)fd, name, mode));
209}
210
211tdata_t
212_TIFFmalloc(tsize_t s)
213{
214 return ((tdata_t)GlobalAlloc(GMEM_FIXED, s));
215}
216
217void
218_TIFFfree(tdata_t p)
219{
220 GlobalFree(p);
221 return;
222}
223
224tdata_t
225_TIFFrealloc(tdata_t p, tsize_t s)
226{
00cb87b4
VZ
227 void* pvTmp;
228 tsize_t old=GlobalSize(p);
229 if (old>=s)
230 {
231 if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
232 CopyMemory(pvTmp, p, s);
233 GlobalFree(p);
234 }
235 }
236 else
237 {
238 if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
239 CopyMemory(pvTmp, p, old);
240 GlobalFree(p);
241 }
242 }
243 return ((tdata_t)pvTmp);
b47c832e
RR
244}
245
246void
247_TIFFmemset(void* p, int v, tsize_t c)
248{
249 FillMemory(p, c, (BYTE)v);
250}
251
252void
253_TIFFmemcpy(void* d, const tdata_t s, tsize_t c)
254{
255 CopyMemory(d, s, c);
256}
257
258int
259_TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
260{
00cb87b4
VZ
261 register const BYTE *pb1 = (const BYTE *) p1;
262 register const BYTE *pb2 = (const BYTE *) p2;
b47c832e
RR
263 register DWORD dwTmp = c;
264 register int iTmp;
265 for (iTmp = 0; dwTmp-- && !iTmp; iTmp = (int)*pb1++ - (int)*pb2++)
266 ;
267 return (iTmp);
268}
269
270static void
271Win32WarningHandler(const char* module, const char* fmt, va_list ap)
272{
273#ifndef TIF_PLATFORM_CONSOLE
274 LPTSTR szTitle;
275 LPTSTR szTmp;
276 LPCTSTR szTitleText = "%s Warning";
277 LPCTSTR szDefaultModule = "TIFFLIB";
278 szTmp = (module == NULL) ? (LPTSTR)szDefaultModule : (LPTSTR)module;
279 if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, (lstrlen(szTmp) +
280 lstrlen(szTitleText) + lstrlen(fmt) + 128)*sizeof(TCHAR))) == NULL)
281 return;
282 wsprintf(szTitle, szTitleText, szTmp);
283 szTmp = szTitle + (lstrlen(szTitle)+2)*sizeof(TCHAR);
284 wvsprintf(szTmp, fmt, ap);
285 MessageBox(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONINFORMATION);
286 LocalFree(szTitle);
287 return;
288#else
289 if (module != NULL)
290 fprintf(stderr, "%s: ", module);
291 fprintf(stderr, "Warning, ");
292 vfprintf(stderr, fmt, ap);
293 fprintf(stderr, ".\n");
294#endif
295}
296TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
297
298static void
299Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
300{
301#ifndef TIF_PLATFORM_CONSOLE
302 LPTSTR szTitle;
303 LPTSTR szTmp;
304 LPCTSTR szTitleText = "%s Error";
305 LPCTSTR szDefaultModule = "TIFFLIB";
306 szTmp = (module == NULL) ? (LPTSTR)szDefaultModule : (LPTSTR)module;
307 if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, (lstrlen(szTmp) +
308 lstrlen(szTitleText) + lstrlen(fmt) + 128)*sizeof(TCHAR))) == NULL)
309 return;
310 wsprintf(szTitle, szTitleText, szTmp);
311 szTmp = szTitle + (lstrlen(szTitle)+2)*sizeof(TCHAR);
312 wvsprintf(szTmp, fmt, ap);
313 MessageBox(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONEXCLAMATION);
314 LocalFree(szTitle);
315 return;
316#else
317 if (module != NULL)
318 fprintf(stderr, "%s: ", module);
319 vfprintf(stderr, fmt, ap);
320 fprintf(stderr, ".\n");
321#endif
322}
323TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;