267
|
1 /* mswindows-specific Lisp objects.
|
280
|
2 Copyright (C) 1998 Andy Piper.
|
267
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: Not in FSF. */
|
|
22
|
|
23 /* written by Andy Piper <andyp@parallax.co.uk> plagerising buts from
|
|
24 glyphs-x.c */
|
|
25
|
|
26 #include <config.h>
|
|
27 #include "lisp.h"
|
|
28 #include "lstream.h"
|
|
29 #include "console-msw.h"
|
|
30 #include "glyphs-msw.h"
|
|
31 #include "objects-msw.h"
|
|
32
|
|
33 #include "buffer.h"
|
|
34 #include "frame.h"
|
|
35 #include "insdel.h"
|
|
36 #include "opaque.h"
|
|
37 #include "sysfile.h"
|
|
38 #include "faces.h"
|
|
39 #include "imgproc.h"
|
|
40
|
|
41 #ifdef FILE_CODING
|
|
42 #include "file-coding.h"
|
|
43 #endif
|
|
44
|
|
45 DEFINE_IMAGE_INSTANTIATOR_FORMAT (bmp);
|
|
46 Lisp_Object Qbmp;
|
|
47 Lisp_Object Vmswindows_bitmap_file_path;
|
282
|
48 static COLORREF transparent_color = RGB (1,1,1);
|
267
|
49
|
|
50 static void
|
|
51 mswindows_initialize_dibitmap_image_instance (struct Lisp_Image_Instance *ii,
|
|
52 enum image_instance_type type);
|
282
|
53 static void
|
|
54 mswindows_initialize_image_instance_mask (struct Lisp_Image_Instance* image,
|
|
55 struct frame* f);
|
267
|
56
|
269
|
57 COLORREF mswindows_string_to_color (CONST char *name);
|
267
|
58
|
|
59 /************************************************************************/
|
|
60 /* convert from a series of RGB triples to a BITMAPINFO formated for the*/
|
|
61 /* proper display */
|
|
62 /************************************************************************/
|
278
|
63 static BITMAPINFO* convert_EImage_to_DIBitmap (Lisp_Object device,
|
|
64 int width, int height,
|
|
65 unsigned char *pic,
|
|
66 int *bit_count,
|
|
67 unsigned char** bmp_data)
|
267
|
68 {
|
|
69 struct device *d = XDEVICE (device);
|
269
|
70 int i,j;
|
267
|
71 RGBQUAD* colortbl;
|
|
72 int ncolors;
|
|
73 BITMAPINFO* bmp_info;
|
269
|
74 unsigned char *ip, *dp;
|
267
|
75
|
269
|
76 if (DEVICE_MSWINDOWS_BITSPIXEL (d) > 0)
|
267
|
77 {
|
269
|
78 int bpline=(int)(~3UL & (unsigned long)((width*3) +3));
|
267
|
79 /* FIXME: we can do this because 24bpp implies no colour table, once
|
|
80 * we start paletizing this is no longer true. The X versions of
|
|
81 * this function quantises to 256 colours or bit masks down to a
|
|
82 * long. Windows can actually handle rgb triples in the raw so I
|
272
|
83 * don't see much point trying to optimize down to the best
|
267
|
84 * structure - unless it has memory / color allocation implications
|
|
85 * .... */
|
269
|
86 bmp_info=xnew_and_zero (BITMAPINFO);
|
267
|
87
|
|
88 if (!bmp_info)
|
|
89 {
|
|
90 return NULL;
|
|
91 }
|
|
92
|
|
93 bmp_info->bmiHeader.biBitCount=24; /* just RGB triples for now */
|
|
94 bmp_info->bmiHeader.biCompression=BI_RGB; /* just RGB triples for now */
|
|
95 bmp_info->bmiHeader.biSizeImage=width*height*3;
|
|
96
|
|
97 /* bitmap data needs to be in blue, green, red triples - in that
|
|
98 order, eimage is in RGB format so we need to convert */
|
272
|
99 *bmp_data = xnew_array_and_zero (unsigned char, bpline * height);
|
278
|
100 *bit_count = bpline * height;
|
267
|
101
|
|
102 if (!bmp_data)
|
|
103 {
|
272
|
104 xfree (bmp_info);
|
267
|
105 return NULL;
|
|
106 }
|
272
|
107
|
|
108 ip = pic;
|
|
109 for (i = height-1; i >= 0; i--) {
|
|
110 dp = (*bmp_data) + (i * bpline);
|
|
111 for (j = 0; j < width; j++) {
|
|
112 dp[2] =*ip++;
|
|
113 dp[1] =*ip++;
|
|
114 *dp =*ip++;
|
|
115 dp += 3;
|
267
|
116 }
|
272
|
117 }
|
267
|
118 }
|
|
119 else /* scale to 256 colors */
|
|
120 {
|
272
|
121 int rd,gr,bl;
|
267
|
122 quant_table *qtable;
|
|
123 int bpline= (int)(~3UL & (unsigned long)(width +3));
|
|
124 /* Quantize the image and get a histogram while we're at it.
|
|
125 Do this first to save memory */
|
269
|
126 qtable = build_EImage_quantable(pic, width, height, 256);
|
267
|
127 if (qtable == NULL) return NULL;
|
|
128
|
|
129 /* use our quantize table to allocate the colors */
|
|
130 ncolors = qtable->num_active_colors;
|
272
|
131 bmp_info=(BITMAPINFO*)xmalloc_and_zero (sizeof(BITMAPINFOHEADER) +
|
267
|
132 sizeof(RGBQUAD) * ncolors);
|
|
133 if (!bmp_info)
|
|
134 {
|
272
|
135 xfree (qtable);
|
267
|
136 return NULL;
|
|
137 }
|
|
138
|
|
139 colortbl=(RGBQUAD*)(((unsigned char*)bmp_info)+sizeof(BITMAPINFOHEADER));
|
|
140
|
|
141 bmp_info->bmiHeader.biBitCount=8;
|
|
142 bmp_info->bmiHeader.biCompression=BI_RGB;
|
|
143 bmp_info->bmiHeader.biSizeImage=bpline*height;
|
|
144 bmp_info->bmiHeader.biClrUsed=ncolors;
|
|
145 bmp_info->bmiHeader.biClrImportant=ncolors;
|
|
146
|
|
147 *bmp_data = (unsigned char *) xmalloc_and_zero (bpline * height);
|
|
148 *bit_count = bpline * height;
|
|
149
|
|
150 if (!*bmp_data)
|
|
151 {
|
269
|
152 xfree (qtable);
|
|
153 xfree (bmp_info);
|
267
|
154 return NULL;
|
|
155 }
|
|
156
|
|
157 /* build up an RGBQUAD colortable */
|
|
158 for (i = 0; i < qtable->num_active_colors; i++) {
|
|
159 colortbl[i].rgbRed = qtable->rm[i];
|
|
160 colortbl[i].rgbGreen = qtable->gm[i];
|
|
161 colortbl[i].rgbBlue = qtable->bm[i];
|
|
162 colortbl[i].rgbReserved = 0;
|
|
163 }
|
|
164
|
|
165 /* now build up the data. picture has to be upside-down and
|
|
166 back-to-front for msw bitmaps */
|
|
167 ip = pic;
|
|
168 for (i = height-1; i >= 0; i--) {
|
|
169 dp = (*bmp_data) + (i * bpline);
|
|
170 for (j = 0; j < width; j++) {
|
|
171 rd = *ip++;
|
|
172 gr = *ip++;
|
|
173 bl = *ip++;
|
269
|
174 *dp++ = QUANT_GET_COLOR (qtable,rd,gr,bl);
|
267
|
175 }
|
|
176 }
|
269
|
177 xfree (qtable);
|
267
|
178 }
|
|
179 /* fix up the standard stuff */
|
|
180 bmp_info->bmiHeader.biWidth=width;
|
|
181 bmp_info->bmiHeader.biHeight=height;
|
|
182 bmp_info->bmiHeader.biPlanes=1;
|
|
183 bmp_info->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
|
269
|
184 bmp_info->bmiHeader.biXPelsPerMeter=0; /* unless you know better */
|
|
185 bmp_info->bmiHeader.biYPelsPerMeter=0;
|
267
|
186
|
|
187 return bmp_info;
|
|
188 }
|
|
189
|
|
190 /* Given a pixmap filename, look through all of the "standard" places
|
|
191 where the file might be located. Return a full pathname if found;
|
|
192 otherwise, return Qnil. */
|
|
193
|
280
|
194 static Lisp_Object
|
278
|
195 mswindows_locate_pixmap_file (Lisp_Object name)
|
267
|
196 {
|
|
197 /* This function can GC if IN_REDISPLAY is false */
|
276
|
198 Lisp_Object found;
|
267
|
199
|
|
200 /* Check non-absolute pathnames with a directory component relative to
|
|
201 the search path; that's the way Xt does it. */
|
269
|
202 if (IS_DIRECTORY_SEP(XSTRING_BYTE (name, 0)) ||
|
267
|
203 (XSTRING_BYTE (name, 0) == '.' &&
|
269
|
204 (IS_DIRECTORY_SEP(XSTRING_BYTE (name, 1)) ||
|
267
|
205 (XSTRING_BYTE (name, 1) == '.' &&
|
269
|
206 (IS_DIRECTORY_SEP(XSTRING_BYTE (name, 2)))))))
|
267
|
207 {
|
|
208 if (!NILP (Ffile_readable_p (name)))
|
|
209 return name;
|
|
210 else
|
|
211 return Qnil;
|
|
212 }
|
|
213
|
276
|
214 if (locate_file (Vmswindows_bitmap_file_path, name, "", &found, R_OK) < 0)
|
|
215 {
|
|
216 Lisp_Object temp = list1 (Vdata_directory);
|
|
217 struct gcpro gcpro1;
|
267
|
218
|
276
|
219 GCPRO1 (temp);
|
|
220 locate_file (temp, name, "", &found, R_OK);
|
|
221 UNGCPRO;
|
|
222 }
|
267
|
223
|
276
|
224 return found;
|
267
|
225 }
|
|
226
|
|
227
|
|
228 /* Initialize an image instance from a bitmap
|
|
229
|
|
230 DEST_MASK specifies the mask of allowed image types.
|
|
231
|
|
232 If this fails, signal an error. INSTANTIATOR is only used
|
|
233 in the error message. */
|
|
234
|
|
235 static void
|
|
236 init_image_instance_from_dibitmap (struct Lisp_Image_Instance *ii,
|
|
237 BITMAPINFO *bmp_info,
|
|
238 int dest_mask,
|
|
239 void *bmp_data,
|
|
240 int bmp_bits,
|
282
|
241 Lisp_Object instantiator,
|
|
242 int x_hot, int y_hot,
|
|
243 int create_mask)
|
267
|
244 {
|
|
245 Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii);
|
|
246 struct device *d = XDEVICE (device);
|
269
|
247 struct frame *f = XFRAME (DEVICE_SELECTED_FRAME (d));
|
267
|
248 void* bmp_buf=0;
|
282
|
249 int type;
|
267
|
250 HBITMAP bitmap;
|
269
|
251 HDC hdc;
|
267
|
252
|
|
253 if (!DEVICE_MSWINDOWS_P (d))
|
|
254 signal_simple_error ("Not an mswindows device", device);
|
|
255
|
|
256 if (NILP (DEVICE_SELECTED_FRAME (d)))
|
|
257 signal_simple_error ("No selected frame on mswindows device", device);
|
|
258
|
282
|
259 if (dest_mask & IMAGE_COLOR_PIXMAP_MASK)
|
|
260 type = IMAGE_COLOR_PIXMAP;
|
|
261 else if (dest_mask & IMAGE_POINTER_MASK)
|
|
262 type = IMAGE_POINTER;
|
|
263 else
|
267
|
264 incompatible_image_types (instantiator, dest_mask,
|
282
|
265 IMAGE_COLOR_PIXMAP_MASK | IMAGE_POINTER_MASK);
|
267
|
266 hdc = FRAME_MSWINDOWS_DC (f);
|
|
267
|
269
|
268 bitmap=CreateDIBSection (hdc,
|
267
|
269 bmp_info,
|
|
270 DIB_RGB_COLORS,
|
|
271 &bmp_buf,
|
|
272 0,0);
|
|
273
|
|
274 if (!bitmap || !bmp_buf)
|
|
275 signal_simple_error ("Unable to create bitmap", instantiator);
|
|
276
|
|
277 /* copy in the actual bitmap */
|
269
|
278 memcpy (bmp_buf, bmp_data, bmp_bits);
|
267
|
279
|
282
|
280 mswindows_initialize_dibitmap_image_instance (ii, type);
|
267
|
281
|
|
282 IMAGE_INSTANCE_PIXMAP_FILENAME (ii) =
|
|
283 find_keyword_in_vector (instantiator, Q_file);
|
|
284
|
|
285 IMAGE_INSTANCE_MSWINDOWS_BITMAP (ii) = bitmap;
|
282
|
286 IMAGE_INSTANCE_MSWINDOWS_MASK (ii) = NULL;
|
267
|
287 IMAGE_INSTANCE_PIXMAP_WIDTH (ii) = bmp_info->bmiHeader.biWidth;
|
|
288 IMAGE_INSTANCE_PIXMAP_HEIGHT (ii) = bmp_info->bmiHeader.biHeight;
|
|
289 IMAGE_INSTANCE_PIXMAP_DEPTH (ii) = bmp_info->bmiHeader.biBitCount;
|
282
|
290 XSETINT (IMAGE_INSTANCE_PIXMAP_HOTSPOT_X (ii), x_hot);
|
|
291 XSETINT (IMAGE_INSTANCE_PIXMAP_HOTSPOT_Y (ii), y_hot);
|
|
292
|
|
293 if (create_mask)
|
|
294 {
|
|
295 mswindows_initialize_image_instance_mask (ii, f);
|
|
296 }
|
|
297
|
|
298 if (type == IMAGE_POINTER)
|
|
299 {
|
|
300 mswindows_initialize_image_instance_icon(ii, TRUE);
|
|
301 }
|
267
|
302 }
|
|
303
|
278
|
304 static void
|
|
305 mswindows_init_image_instance_from_eimage (struct Lisp_Image_Instance *ii,
|
|
306 int width, int height,
|
|
307 unsigned char *eimage,
|
|
308 int dest_mask,
|
|
309 Lisp_Object instantiator,
|
|
310 Lisp_Object domain)
|
|
311 {
|
|
312 Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii);
|
|
313 BITMAPINFO* bmp_info;
|
|
314 unsigned char* bmp_data;
|
|
315 int bmp_bits;
|
|
316 COLORREF bkcolor;
|
|
317
|
|
318 if (!DEVICE_MSWINDOWS_P (XDEVICE (device)))
|
|
319 signal_simple_error ("Not an mswindows device", device);
|
|
320
|
|
321 /* this is a hack but MaskBlt and TransparentBlt are not supported
|
|
322 on most windows variants */
|
|
323 bkcolor = COLOR_INSTANCE_MSWINDOWS_COLOR
|
|
324 (XCOLOR_INSTANCE (FACE_BACKGROUND (Vdefault_face, domain)));
|
|
325
|
|
326 /* build a bitmap from the eimage */
|
|
327 if (!(bmp_info=convert_EImage_to_DIBitmap (device, width, height, eimage,
|
|
328 &bmp_bits, &bmp_data)))
|
|
329 {
|
|
330 signal_simple_error ("EImage to DIBitmap conversion failed",
|
|
331 instantiator);
|
|
332 }
|
|
333
|
|
334 /* Now create the pixmap and set up the image instance */
|
|
335 init_image_instance_from_dibitmap (ii, bmp_info, dest_mask,
|
282
|
336 bmp_data, bmp_bits, instantiator,
|
|
337 0, 0, 0);
|
278
|
338
|
|
339 xfree (bmp_info);
|
|
340 xfree (bmp_data);
|
|
341 }
|
|
342
|
282
|
343 static void set_mono_pixel ( unsigned char* bits,
|
|
344 int bpline, int height,
|
|
345 int x, int y, int white )
|
|
346 {
|
|
347 int index;
|
|
348 unsigned char bitnum;
|
|
349 /* Find the byte on which this scanline begins */
|
|
350 index = (height - y - 1) * bpline;
|
|
351 /* Find the byte containing this pixel */
|
|
352 index += (x >> 3);
|
|
353 /* Which bit is it? */
|
|
354 bitnum = (unsigned char)( 7 - (x % 8) );
|
|
355 if( white ) /* Turn it on */
|
|
356 bits[index] |= (1<<bitnum);
|
|
357 else /* Turn it off */
|
|
358 bits[index] &= ~(1<<bitnum);
|
|
359 }
|
|
360
|
|
361 static void
|
|
362 mswindows_initialize_image_instance_mask (struct Lisp_Image_Instance* image,
|
|
363 struct frame* f)
|
276
|
364 {
|
|
365 HBITMAP mask, bmp;
|
|
366 HDC hcdc = FRAME_MSWINDOWS_CDC (f);
|
282
|
367 BITMAPINFO* bmp_info =
|
|
368 xmalloc_and_zero (sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD));
|
|
369 int i, j;
|
|
370 int height = IMAGE_INSTANCE_PIXMAP_HEIGHT (image);
|
276
|
371
|
282
|
372 void* and_bits;
|
|
373 int bpline= (int)(~3UL & (unsigned long)
|
|
374 (((IMAGE_INSTANCE_PIXMAP_WIDTH (image)+7)/8) +3));
|
|
375
|
|
376 bmp_info->bmiHeader.biWidth=IMAGE_INSTANCE_PIXMAP_WIDTH (image);
|
|
377 bmp_info->bmiHeader.biHeight = height;
|
|
378 bmp_info->bmiHeader.biPlanes=1;
|
|
379 bmp_info->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
|
|
380 bmp_info->bmiHeader.biBitCount=1;
|
|
381 bmp_info->bmiHeader.biCompression=BI_RGB;
|
|
382 bmp_info->bmiHeader.biClrUsed = 2;
|
|
383 bmp_info->bmiHeader.biClrImportant = 2;
|
|
384 bmp_info->bmiHeader.biSizeImage = height * bpline;
|
|
385 bmp_info->bmiColors[0].rgbRed = 0;
|
|
386 bmp_info->bmiColors[0].rgbGreen = 0;
|
|
387 bmp_info->bmiColors[0].rgbBlue = 0;
|
|
388 bmp_info->bmiColors[0].rgbReserved = 0;
|
|
389 bmp_info->bmiColors[1].rgbRed = 255;
|
|
390 bmp_info->bmiColors[1].rgbGreen = 255;
|
|
391 bmp_info->bmiColors[1].rgbBlue = 255;
|
|
392 bmp_info->bmiColors[0].rgbReserved = 0;
|
|
393
|
|
394 if (!(mask = CreateDIBSection (hcdc,
|
|
395 bmp_info,
|
|
396 DIB_RGB_COLORS,
|
|
397 &and_bits,
|
|
398 0,0)))
|
276
|
399 {
|
282
|
400 xfree (bmp_info);
|
|
401 return;
|
276
|
402 }
|
|
403
|
282
|
404 xfree (bmp_info);
|
|
405 SelectObject (hcdc, IMAGE_INSTANCE_MSWINDOWS_BITMAP (image));
|
276
|
406
|
282
|
407 for(i=0; i<IMAGE_INSTANCE_PIXMAP_WIDTH (image); i++)
|
|
408 {
|
|
409 for(j=0; j<height; j++)
|
|
410 {
|
|
411 if( GetPixel( hcdc, i, j ) == transparent_color )
|
|
412 {
|
|
413 SetPixel( hcdc, i, j, RGB (0,0,0));
|
|
414 set_mono_pixel( and_bits, bpline, height, i, j, TRUE );
|
|
415 }
|
|
416 else
|
|
417 {
|
|
418 set_mono_pixel( and_bits, bpline, height, i, j, FALSE );
|
|
419 }
|
|
420 }
|
276
|
421 }
|
|
422
|
282
|
423 GdiFlush();
|
276
|
424 SelectObject(hcdc, 0);
|
282
|
425
|
|
426 IMAGE_INSTANCE_MSWINDOWS_MASK (image) = mask;
|
|
427 }
|
|
428
|
|
429 void
|
|
430 mswindows_initialize_image_instance_icon (struct Lisp_Image_Instance* image,
|
|
431 int cursor)
|
|
432 {
|
|
433 ICONINFO x_icon;
|
|
434
|
|
435 /* we rely on windows to do any resizing necessary */
|
|
436 x_icon.fIcon=cursor ? FALSE : TRUE;
|
|
437 x_icon.xHotspot=XINT (IMAGE_INSTANCE_PIXMAP_HOTSPOT_X (image));
|
|
438 x_icon.yHotspot=XINT (IMAGE_INSTANCE_PIXMAP_HOTSPOT_Y (image));
|
|
439 x_icon.hbmMask=IMAGE_INSTANCE_MSWINDOWS_MASK (image);
|
|
440 x_icon.hbmColor=IMAGE_INSTANCE_MSWINDOWS_BITMAP (image);
|
276
|
441
|
282
|
442 IMAGE_INSTANCE_MSWINDOWS_ICON (image)=
|
276
|
443 CreateIconIndirect (&x_icon);
|
|
444 }
|
|
445
|
|
446 int
|
|
447 mswindows_resize_dibitmap_instance (struct Lisp_Image_Instance* ii,
|
|
448 struct frame* f,
|
|
449 int newx, int newy)
|
|
450 {
|
|
451 HBITMAP newbmp;
|
|
452 HDC hcdc = FRAME_MSWINDOWS_CDC (f);
|
|
453 HDC hdcDst = CreateCompatibleDC (hcdc);
|
|
454
|
|
455 SelectObject(hcdc, IMAGE_INSTANCE_MSWINDOWS_BITMAP (ii));
|
|
456
|
|
457 newbmp = CreateCompatibleBitmap(hcdc, newx, newy);
|
|
458
|
|
459 DeleteObject( SelectObject(hdcDst, newbmp) );
|
|
460
|
|
461 if (!StretchBlt(hdcDst, 0, 0, newx, newy,
|
|
462 hcdc, 0, 0,
|
|
463 IMAGE_INSTANCE_PIXMAP_WIDTH (ii),
|
|
464 IMAGE_INSTANCE_PIXMAP_HEIGHT (ii),
|
|
465 SRCCOPY))
|
|
466 {
|
|
467 return FALSE;
|
|
468 }
|
|
469
|
|
470 SelectObject(hdcDst, 0);
|
|
471 SelectObject(hcdc, 0);
|
|
472
|
|
473 if (IMAGE_INSTANCE_MSWINDOWS_BITMAP (ii))
|
|
474 DeleteObject (IMAGE_INSTANCE_MSWINDOWS_BITMAP (ii));
|
|
475 if (IMAGE_INSTANCE_MSWINDOWS_MASK (ii))
|
|
476 DeleteObject (IMAGE_INSTANCE_MSWINDOWS_MASK (ii));
|
|
477
|
|
478 IMAGE_INSTANCE_MSWINDOWS_BITMAP (ii) = newbmp;
|
|
479 IMAGE_INSTANCE_MSWINDOWS_MASK (ii) = newbmp;
|
|
480 IMAGE_INSTANCE_PIXMAP_WIDTH (ii) = newx;
|
|
481 IMAGE_INSTANCE_PIXMAP_HEIGHT (ii) = newy;
|
|
482
|
|
483 DeleteDC(hdcDst);
|
|
484
|
|
485 return TRUE;
|
|
486 }
|
|
487
|
267
|
488 /**********************************************************************
|
|
489 * XPM *
|
|
490 **********************************************************************/
|
|
491
|
|
492 #ifdef HAVE_XPM
|
280
|
493
|
|
494 struct color_symbol
|
|
495 {
|
|
496 char* name;
|
|
497 COLORREF color;
|
|
498 };
|
|
499
|
|
500 static struct color_symbol*
|
|
501 extract_xpm_color_names (Lisp_Object device,
|
|
502 Lisp_Object domain,
|
|
503 Lisp_Object color_symbol_alist,
|
|
504 int* nsymbols)
|
|
505 {
|
|
506 /* This function can GC */
|
|
507 Lisp_Object rest;
|
|
508 Lisp_Object results = Qnil;
|
|
509 int i, j;
|
|
510 struct color_symbol *colortbl;
|
|
511 struct gcpro gcpro1, gcpro2;
|
|
512
|
|
513 GCPRO2 (results, device);
|
|
514
|
|
515 /* We built up results to be (("name" . #<color>) ...) so that if an
|
|
516 error happens we don't lose any malloc()ed data, or more importantly,
|
|
517 leave any pixels allocated in the server. */
|
|
518 i = 0;
|
|
519 LIST_LOOP (rest, color_symbol_alist)
|
|
520 {
|
|
521 Lisp_Object cons = XCAR (rest);
|
|
522 Lisp_Object name = XCAR (cons);
|
|
523 Lisp_Object value = XCDR (cons);
|
|
524 if (NILP (value))
|
|
525 continue;
|
|
526 if (STRINGP (value))
|
|
527 value =
|
|
528 Fmake_color_instance
|
|
529 (value, device, encode_error_behavior_flag (ERROR_ME_NOT));
|
|
530 else
|
|
531 {
|
|
532 assert (COLOR_SPECIFIERP (value));
|
|
533 value = Fspecifier_instance (value, domain, Qnil, Qnil);
|
|
534 }
|
|
535 if (NILP (value))
|
|
536 continue;
|
|
537 results = noseeum_cons (noseeum_cons (name, value), results);
|
|
538 i++;
|
|
539 }
|
|
540 UNGCPRO; /* no more evaluation */
|
|
541
|
|
542 *nsymbols=i;
|
|
543 if (i == 0) return 0;
|
|
544
|
|
545 colortbl = xnew_array_and_zero (struct color_symbol, i);
|
|
546
|
|
547 for (j=0; j<i; j++)
|
|
548 {
|
|
549 Lisp_Object cons = XCAR (results);
|
|
550 colortbl[j].color =
|
|
551 COLOR_INSTANCE_MSWINDOWS_COLOR (XCOLOR_INSTANCE (XCDR (cons)));
|
|
552
|
|
553 colortbl[j].name = (char *) XSTRING_DATA (XCAR (cons));
|
|
554 free_cons (XCONS (cons));
|
|
555 cons = results;
|
|
556 results = XCDR (results);
|
|
557 free_cons (XCONS (cons));
|
|
558 }
|
|
559 return colortbl;
|
|
560 }
|
|
561
|
269
|
562 static int xpm_to_eimage (Lisp_Object image, CONST Extbyte *buffer,
|
276
|
563 unsigned char** data,
|
|
564 int* width, int* height,
|
|
565 int* x_hot, int* y_hot,
|
282
|
566 int* transp,
|
|
567 struct color_symbol* color_symbols,
|
280
|
568 int nsymbols)
|
267
|
569 {
|
|
570 XpmImage xpmimage;
|
|
571 XpmInfo xpminfo;
|
280
|
572 int result, i, j, transp_idx, maskbpline;
|
267
|
573 unsigned char* dptr;
|
|
574 unsigned int* sptr;
|
|
575 COLORREF color; /* the american spelling virus hits again .. */
|
|
576 COLORREF* colortbl;
|
|
577
|
272
|
578 xzero (xpmimage);
|
|
579 xzero (xpminfo);
|
276
|
580 xpminfo.valuemask=XpmHotspot;
|
282
|
581 *transp=FALSE;
|
276
|
582
|
269
|
583 result = XpmCreateXpmImageFromBuffer ((char*)buffer,
|
267
|
584 &xpmimage,
|
|
585 &xpminfo);
|
269
|
586 switch (result)
|
267
|
587 {
|
|
588 case XpmSuccess:
|
|
589 break;
|
|
590 case XpmFileInvalid:
|
|
591 {
|
|
592 signal_simple_error ("invalid XPM data", image);
|
|
593 }
|
|
594 case XpmNoMemory:
|
|
595 {
|
|
596 signal_double_file_error ("Parsing pixmap data",
|
|
597 "out of memory", image);
|
|
598 }
|
|
599 default:
|
|
600 {
|
|
601 signal_double_file_error_2 ("Parsing pixmap data",
|
|
602 "unknown error code",
|
|
603 make_int (result), image);
|
|
604 }
|
|
605 }
|
|
606
|
|
607 *width = xpmimage.width;
|
|
608 *height = xpmimage.height;
|
276
|
609 maskbpline = (int)(~3UL & (unsigned long)
|
|
610 (((~7UL & (unsigned long)(*width + 7)) / 8) + 3));
|
|
611
|
|
612 *data = xnew_array_and_zero (unsigned char, *width * *height * 3);
|
267
|
613
|
|
614 if (!*data)
|
|
615 {
|
269
|
616 XpmFreeXpmImage (&xpmimage);
|
|
617 XpmFreeXpmInfo (&xpminfo);
|
267
|
618 return 0;
|
|
619 }
|
|
620
|
|
621 /* build a color table to speed things up */
|
|
622 colortbl = xnew_array_and_zero (COLORREF, xpmimage.ncolors);
|
|
623 if (!colortbl)
|
|
624 {
|
269
|
625 xfree (*data);
|
|
626 XpmFreeXpmImage (&xpmimage);
|
|
627 XpmFreeXpmInfo (&xpminfo);
|
267
|
628 return 0;
|
|
629 }
|
|
630
|
|
631 for (i=0; i<xpmimage.ncolors; i++)
|
|
632 {
|
280
|
633 /* pick up symbolic colors */
|
|
634 if (xpmimage.colorTable[i].c_color == 0
|
|
635 &&
|
|
636 xpmimage.colorTable[i].symbolic != 0)
|
|
637 {
|
|
638 if (!color_symbols)
|
|
639 {
|
|
640 xfree (*data);
|
|
641 xfree (colortbl);
|
|
642 XpmFreeXpmImage (&xpmimage);
|
|
643 XpmFreeXpmInfo (&xpminfo);
|
|
644 return 0;
|
|
645 }
|
|
646 for (j = 0; j<nsymbols; j++)
|
|
647 {
|
|
648 if (!strcmp (xpmimage.colorTable[i].symbolic,
|
|
649 color_symbols[j].name ))
|
|
650 {
|
|
651 colortbl[i]=color_symbols[j].color;
|
|
652 }
|
|
653 }
|
|
654 }
|
267
|
655 /* pick up transparencies */
|
282
|
656 else if (!strcasecmp (xpmimage.colorTable[i].c_color,"None")
|
|
657 ||
|
|
658 xpmimage.colorTable[i].symbolic
|
|
659 &&
|
|
660 (!strcasecmp (xpmimage.colorTable[i].symbolic,"BgColor")
|
|
661 ||
|
|
662 !strcasecmp (xpmimage.colorTable[i].symbolic,"None")))
|
267
|
663 {
|
282
|
664 *transp=TRUE;
|
|
665 colortbl[i]=transparent_color;
|
276
|
666 transp_idx=i;
|
267
|
667 }
|
|
668 else
|
|
669 {
|
|
670 colortbl[i]=
|
269
|
671 mswindows_string_to_color (xpmimage.colorTable[i].c_color);
|
267
|
672 }
|
|
673 }
|
|
674
|
|
675 /* convert the image */
|
|
676 sptr=xpmimage.data;
|
|
677 dptr=*data;
|
|
678 for (i = 0; i< *width * *height; i++)
|
|
679 {
|
|
680 color = colortbl[*sptr++];
|
|
681
|
|
682 /* split out the 0x02bbggrr colorref into an rgb triple */
|
269
|
683 *dptr++=GetRValue (color); /* red */
|
|
684 *dptr++=GetGValue (color); /* green */
|
|
685 *dptr++=GetBValue (color); /* blue */
|
267
|
686 }
|
|
687
|
276
|
688 *x_hot=xpminfo.x_hotspot;
|
|
689 *y_hot=xpminfo.y_hotspot;
|
|
690
|
269
|
691 XpmFreeXpmImage (&xpmimage);
|
|
692 XpmFreeXpmInfo (&xpminfo);
|
|
693 xfree (colortbl);
|
267
|
694 return TRUE;
|
|
695 }
|
|
696
|
280
|
697 static void
|
267
|
698 mswindows_xpm_instantiate (Lisp_Object image_instance,
|
|
699 Lisp_Object instantiator,
|
|
700 Lisp_Object pointer_fg, Lisp_Object pointer_bg,
|
|
701 int dest_mask, Lisp_Object domain)
|
|
702 {
|
|
703 struct Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
|
|
704 Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii);
|
|
705 CONST Extbyte *bytes;
|
|
706 Extcount len;
|
|
707 unsigned char *eimage;
|
276
|
708 int width, height, x_hot, y_hot;
|
267
|
709 BITMAPINFO* bmp_info;
|
|
710 unsigned char* bmp_data;
|
|
711 int bmp_bits;
|
|
712 COLORREF bkcolor;
|
282
|
713 int nsymbols=0, transp;
|
280
|
714 struct color_symbol* color_symbols=NULL;
|
267
|
715
|
|
716 Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
|
280
|
717 Lisp_Object color_symbol_alist = find_keyword_in_vector (instantiator,
|
|
718 Q_color_symbols);
|
267
|
719
|
|
720 if (!DEVICE_MSWINDOWS_P (XDEVICE (device)))
|
|
721 signal_simple_error ("Not an mswindows device", device);
|
|
722
|
|
723 assert (!NILP (data));
|
|
724
|
|
725 GET_STRING_BINARY_DATA_ALLOCA (data, bytes, len);
|
|
726
|
280
|
727 /* in case we have color symbols */
|
|
728 color_symbols = extract_xpm_color_names (device, domain,
|
|
729 color_symbol_alist, &nsymbols);
|
|
730
|
267
|
731 /* convert to an eimage to make processing easier */
|
269
|
732 if (!xpm_to_eimage (image_instance, bytes, &eimage, &width, &height,
|
282
|
733 &x_hot, &y_hot, &transp, color_symbols, nsymbols))
|
267
|
734 {
|
|
735 signal_simple_error ("XPM to EImage conversion failed",
|
|
736 image_instance);
|
|
737 }
|
|
738
|
280
|
739 if (color_symbols)
|
|
740 xfree(color_symbols);
|
|
741
|
267
|
742 /* build a bitmap from the eimage */
|
278
|
743 if (!(bmp_info=convert_EImage_to_DIBitmap (device, width, height, eimage,
|
|
744 &bmp_bits, &bmp_data)))
|
267
|
745 {
|
|
746 signal_simple_error ("XPM to EImage conversion failed",
|
|
747 image_instance);
|
|
748 }
|
269
|
749 xfree (eimage);
|
267
|
750
|
|
751 /* Now create the pixmap and set up the image instance */
|
|
752 init_image_instance_from_dibitmap (ii, bmp_info, dest_mask,
|
282
|
753 bmp_data, bmp_bits, instantiator,
|
|
754 x_hot, y_hot, transp);
|
267
|
755
|
269
|
756 xfree (bmp_info);
|
|
757 xfree (bmp_data);
|
267
|
758 }
|
|
759 #endif /* HAVE_XPM */
|
|
760
|
|
761 /**********************************************************************
|
|
762 * BMP *
|
|
763 **********************************************************************/
|
|
764
|
|
765 static void
|
|
766 bmp_validate (Lisp_Object instantiator)
|
|
767 {
|
|
768 file_or_data_must_be_present (instantiator);
|
|
769 }
|
|
770
|
|
771 static Lisp_Object
|
|
772 bmp_normalize (Lisp_Object inst, Lisp_Object console_type)
|
|
773 {
|
|
774 return simple_image_type_normalize (inst, console_type, Qbmp);
|
|
775 }
|
|
776
|
|
777 static int
|
|
778 bmp_possible_dest_types (void)
|
|
779 {
|
|
780 return IMAGE_COLOR_PIXMAP_MASK;
|
|
781 }
|
|
782
|
|
783 static void
|
|
784 bmp_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
|
|
785 Lisp_Object pointer_fg, Lisp_Object pointer_bg,
|
|
786 int dest_mask, Lisp_Object domain)
|
|
787 {
|
|
788 struct Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
|
|
789 Lisp_Object device = IMAGE_INSTANCE_DEVICE (ii);
|
|
790 CONST Extbyte *bytes;
|
|
791 Extcount len;
|
|
792 BITMAPFILEHEADER* bmp_file_header;
|
|
793 BITMAPINFO* bmp_info;
|
|
794 void* bmp_data;
|
|
795 int bmp_bits;
|
|
796 Lisp_Object data = find_keyword_in_vector (instantiator, Q_data);
|
|
797
|
|
798 if (!DEVICE_MSWINDOWS_P (XDEVICE (device)))
|
|
799 signal_simple_error ("Not an mswindows device", device);
|
|
800
|
|
801 assert (!NILP (data));
|
|
802
|
|
803 GET_STRING_BINARY_DATA_ALLOCA (data, bytes, len);
|
|
804
|
|
805 /* Then slurp the image into memory, decoding along the way.
|
|
806 The result is the image in a simple one-byte-per-pixel
|
|
807 format. */
|
|
808
|
|
809 bmp_file_header=(BITMAPFILEHEADER*)bytes;
|
|
810 bmp_info = (BITMAPINFO*)(bytes + sizeof(BITMAPFILEHEADER));
|
|
811 bmp_data = (Extbyte*)bytes + bmp_file_header->bfOffBits;
|
|
812 bmp_bits = bmp_file_header->bfSize - bmp_file_header->bfOffBits;
|
|
813
|
|
814 /* Now create the pixmap and set up the image instance */
|
|
815 init_image_instance_from_dibitmap (ii, bmp_info, dest_mask,
|
282
|
816 bmp_data, bmp_bits, instantiator,
|
|
817 0, 0, 0);
|
267
|
818 }
|
|
819
|
|
820
|
|
821 /************************************************************************/
|
|
822 /* image instance methods */
|
|
823 /************************************************************************/
|
|
824
|
|
825 static void
|
|
826 mswindows_print_image_instance (struct Lisp_Image_Instance *p,
|
|
827 Lisp_Object printcharfun,
|
|
828 int escapeflag)
|
|
829 {
|
|
830 char buf[100];
|
|
831
|
|
832 switch (IMAGE_INSTANCE_TYPE (p))
|
|
833 {
|
|
834 case IMAGE_MONO_PIXMAP:
|
|
835 case IMAGE_COLOR_PIXMAP:
|
|
836 case IMAGE_POINTER:
|
|
837 sprintf (buf, " (0x%lx",
|
|
838 (unsigned long) IMAGE_INSTANCE_MSWINDOWS_BITMAP (p));
|
|
839 write_c_string (buf, printcharfun);
|
|
840 if (IMAGE_INSTANCE_MSWINDOWS_MASK (p))
|
|
841 {
|
|
842 sprintf (buf, "/0x%lx",
|
|
843 (unsigned long) IMAGE_INSTANCE_MSWINDOWS_MASK (p));
|
|
844 write_c_string (buf, printcharfun);
|
|
845 }
|
|
846 write_c_string (")", printcharfun);
|
|
847 break;
|
|
848 default:
|
|
849 break;
|
|
850 }
|
|
851 }
|
|
852
|
|
853 static void
|
|
854 mswindows_finalize_image_instance (struct Lisp_Image_Instance *p)
|
|
855 {
|
|
856 if (!p->data)
|
|
857 return;
|
|
858
|
|
859 if (DEVICE_LIVE_P (XDEVICE (p->device)))
|
|
860 {
|
|
861 if (IMAGE_INSTANCE_MSWINDOWS_BITMAP (p))
|
269
|
862 DeleteObject (IMAGE_INSTANCE_MSWINDOWS_BITMAP (p));
|
267
|
863 IMAGE_INSTANCE_MSWINDOWS_BITMAP (p) = 0;
|
272
|
864 if (IMAGE_INSTANCE_MSWINDOWS_MASK (p))
|
|
865 DeleteObject (IMAGE_INSTANCE_MSWINDOWS_MASK (p));
|
|
866 IMAGE_INSTANCE_MSWINDOWS_MASK (p) = 0;
|
|
867 if (IMAGE_INSTANCE_MSWINDOWS_ICON (p))
|
|
868 DestroyIcon (IMAGE_INSTANCE_MSWINDOWS_ICON (p));
|
|
869 IMAGE_INSTANCE_MSWINDOWS_ICON (p) = 0;
|
267
|
870 }
|
|
871
|
|
872 xfree (p->data);
|
|
873 p->data = 0;
|
|
874 }
|
|
875
|
|
876 static int
|
|
877 mswindows_image_instance_equal (struct Lisp_Image_Instance *p1,
|
|
878 struct Lisp_Image_Instance *p2, int depth)
|
|
879 {
|
|
880 switch (IMAGE_INSTANCE_TYPE (p1))
|
|
881 {
|
|
882 case IMAGE_MONO_PIXMAP:
|
|
883 case IMAGE_COLOR_PIXMAP:
|
|
884 case IMAGE_POINTER:
|
|
885 if (IMAGE_INSTANCE_MSWINDOWS_BITMAP (p1)
|
|
886 != IMAGE_INSTANCE_MSWINDOWS_BITMAP (p2))
|
|
887 return 0;
|
|
888 break;
|
|
889 default:
|
|
890 break;
|
|
891 }
|
|
892
|
|
893 return 1;
|
|
894 }
|
|
895
|
|
896 static unsigned long
|
|
897 mswindows_image_instance_hash (struct Lisp_Image_Instance *p, int depth)
|
|
898 {
|
|
899 switch (IMAGE_INSTANCE_TYPE (p))
|
|
900 {
|
|
901 case IMAGE_MONO_PIXMAP:
|
|
902 case IMAGE_COLOR_PIXMAP:
|
|
903 case IMAGE_POINTER:
|
|
904 return (unsigned long) IMAGE_INSTANCE_MSWINDOWS_BITMAP (p);
|
|
905 default:
|
|
906 return 0;
|
|
907 }
|
|
908 }
|
|
909
|
|
910 /* Set all the slots in an image instance structure to reasonable
|
|
911 default values. This is used somewhere within an instantiate
|
|
912 method. It is assumed that the device slot within the image
|
|
913 instance is already set -- this is the case when instantiate
|
|
914 methods are called. */
|
|
915
|
|
916 static void
|
|
917 mswindows_initialize_dibitmap_image_instance (struct Lisp_Image_Instance *ii,
|
|
918 enum image_instance_type type)
|
|
919 {
|
|
920 ii->data = xnew_and_zero (struct mswindows_image_instance_data);
|
|
921 IMAGE_INSTANCE_TYPE (ii) = type;
|
|
922 IMAGE_INSTANCE_PIXMAP_FILENAME (ii) = Qnil;
|
|
923 IMAGE_INSTANCE_PIXMAP_MASK_FILENAME (ii) = Qnil;
|
|
924 IMAGE_INSTANCE_PIXMAP_HOTSPOT_X (ii) = Qnil;
|
|
925 IMAGE_INSTANCE_PIXMAP_HOTSPOT_Y (ii) = Qnil;
|
|
926 IMAGE_INSTANCE_PIXMAP_FG (ii) = Qnil;
|
|
927 IMAGE_INSTANCE_PIXMAP_BG (ii) = Qnil;
|
|
928 }
|
|
929
|
|
930
|
|
931 /************************************************************************/
|
|
932 /* initialization */
|
|
933 /************************************************************************/
|
|
934
|
|
935 void
|
|
936 syms_of_glyphs_mswindows (void)
|
|
937 {
|
|
938 }
|
|
939
|
|
940 void
|
|
941 console_type_create_glyphs_mswindows (void)
|
|
942 {
|
|
943 /* image methods */
|
|
944
|
|
945 CONSOLE_HAS_METHOD (mswindows, print_image_instance);
|
|
946 CONSOLE_HAS_METHOD (mswindows, finalize_image_instance);
|
|
947 CONSOLE_HAS_METHOD (mswindows, image_instance_equal);
|
|
948 CONSOLE_HAS_METHOD (mswindows, image_instance_hash);
|
278
|
949 CONSOLE_HAS_METHOD (mswindows, init_image_instance_from_eimage);
|
|
950 CONSOLE_HAS_METHOD (mswindows, locate_pixmap_file);
|
280
|
951 #ifdef HAVE_XPM
|
|
952 CONSOLE_HAS_METHOD (mswindows, xpm_instantiate);
|
|
953 #endif
|
267
|
954 }
|
|
955
|
|
956 void
|
|
957 image_instantiator_format_create_glyphs_mswindows (void)
|
|
958 {
|
|
959 /* image-instantiator types */
|
|
960
|
|
961 INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (bmp, "bmp");
|
|
962
|
|
963 IIFORMAT_HAS_METHOD (bmp, validate);
|
|
964 IIFORMAT_HAS_METHOD (bmp, normalize);
|
|
965 IIFORMAT_HAS_METHOD (bmp, possible_dest_types);
|
|
966 IIFORMAT_HAS_METHOD (bmp, instantiate);
|
|
967
|
|
968 IIFORMAT_VALID_KEYWORD (bmp, Q_data, check_valid_string);
|
|
969 IIFORMAT_VALID_KEYWORD (bmp, Q_file, check_valid_string);
|
|
970 }
|
|
971
|
|
972 void
|
|
973 vars_of_glyphs_mswindows (void)
|
|
974 {
|
|
975 Fprovide (Qbmp);
|
|
976 DEFVAR_LISP ("mswindows-bitmap-file-path", &Vmswindows_bitmap_file_path /*
|
|
977 A list of the directories in which mswindows bitmap files may be found.
|
|
978 This is used by the `make-image-instance' function.
|
|
979 */ );
|
|
980 Vmswindows_bitmap_file_path = Qnil;
|
|
981 }
|
|
982
|
|
983 void
|
|
984 complex_vars_of_glyphs_mswindows (void)
|
|
985 {
|
|
986 }
|