Test bits with '&' instead of '&&'.
[wine] / graphics / x11drv / oembitmap.c
1 /*
2  * X11DRV OEM bitmap objects
3  *
4  * Copyright 1994, 1995 Alexandre Julliard
5  *
6  */
7
8 #include "config.h"
9
10 #include "ts_xlib.h"
11 #include "ts_xutil.h"
12
13 #ifdef HAVE_LIBXXPM
14 #include "ts_xpm.h"
15 #else /* defined(HAVE_LIBXXPM) */
16 typedef unsigned long Pixel;
17 #endif /* defined(HAVE_LIBXXPM) */
18
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "winbase.h"
23 #include "wingdi.h"
24 #include "winuser.h"
25 #include "wine/winuser16.h"
26
27 #include "bitmap.h"
28 #include "cursoricon.h"
29 #include "debugtools.h"
30 #include "gdi.h"
31 #include "user.h" /* for TWEAK_WineLook (FIXME) */
32 #include "x11drv.h"
33
34   /* Include OEM pixmaps */
35 #include "bitmaps/obm_lfarrowi"
36 #include "bitmaps/obm_rgarrowi"
37 #include "bitmaps/obm_dnarrowi"
38 #include "bitmaps/obm_uparrowi"
39 #include "bitmaps/obm_combo"
40 #include "bitmaps/obm_mnarrow"
41 #include "bitmaps/obm_lfarrowd"
42 #include "bitmaps/obm_rgarrowd"
43 #include "bitmaps/obm_dnarrowd"
44 #include "bitmaps/obm_uparrowd"
45 #include "bitmaps/obm_restored"
46 #include "bitmaps/obm_restore"
47 #include "bitmaps/obm_lfarrow"
48 #include "bitmaps/obm_rgarrow"
49 #include "bitmaps/obm_dnarrow"
50 #include "bitmaps/obm_uparrow"
51 #include "bitmaps/obm_old_restore"
52 #include "bitmaps/obm_old_zoom"
53 #include "bitmaps/obm_old_reduce"
54 #include "bitmaps/obm_btncorners"
55 #include "bitmaps/obm_checkboxes"
56 #include "bitmaps/obm_check"
57 #include "bitmaps/obm_btsize"
58 #include "bitmaps/obm_old_lfarrow"
59 #include "bitmaps/obm_old_rgarrow"
60 #include "bitmaps/obm_old_dnarrow"
61 #include "bitmaps/obm_old_uparrow"
62 #include "bitmaps/obm_size"
63 #include "bitmaps/obm_old_close"
64 #include "bitmaps/obm_trtype"
65
66 #include "bitmaps/obm_zoomd"
67 #include "bitmaps/obm_reduced"
68 #include "bitmaps/obm_zoom"
69 #include "bitmaps/obm_reduce"
70 #include "bitmaps/obm_close"
71 #include "bitmaps/obm_zoomd_95"
72 #include "bitmaps/obm_reduced_95"
73 #include "bitmaps/obm_zoom_95"
74 #include "bitmaps/obm_reduce_95"
75 #include "bitmaps/obm_close_95"
76 #include "bitmaps/obm_closed_95"
77 #include "bitmaps/obm_restore_95"
78 #include "bitmaps/obm_restored_95"
79
80 DECLARE_DEBUG_CHANNEL(bitmap);
81 DECLARE_DEBUG_CHANNEL(cursor);
82 DECLARE_DEBUG_CHANNEL(x11drv);
83
84
85 #define OBM_FIRST  OBM_CLOSED  /* First OEM bitmap */
86 #define OBM_LAST   OBM_OLD_CLOSE   /* Last OEM bitmap */
87
88 static struct
89 {
90     char** data;   /* Pointer to bitmap data */
91     BOOL color;  /* Is it a color bitmap?  */
92 } OBM_Pixmaps_Data[OBM_LAST-OBM_FIRST+1] = {
93     { obm_closed_95,TRUE},      /* OBM_CLOSED */
94     { obm_trtype, TRUE },       /* OBM_TRTYPE */
95     { NULL, FALSE },            /* unused */
96     { obm_lfarrowi, TRUE },     /* OBM_LFARROWI */
97     { obm_rgarrowi, TRUE },     /* OBM_RGARROWI */
98     { obm_dnarrowi, TRUE },     /* OBM_DNARROWI */
99     { obm_uparrowi, TRUE },     /* OBM_UPARROWI */
100     { obm_combo, FALSE },       /* OBM_COMBO */
101     { obm_mnarrow, FALSE },     /* OBM_MNARROW */
102     { obm_lfarrowd, TRUE },     /* OBM_LFARROWD */
103     { obm_rgarrowd, TRUE },     /* OBM_RGARROWD */
104     { obm_dnarrowd, TRUE },     /* OBM_DNARROWD */
105     { obm_uparrowd, TRUE },     /* OBM_UPARROWD */
106     { obm_restored, TRUE },     /* OBM_RESTORED */
107     { obm_zoomd, TRUE },        /* OBM_ZOOMD */
108     { obm_reduced, TRUE },      /* OBM_REDUCED */
109     { obm_restore, TRUE },      /* OBM_RESTORE */
110     { obm_zoom, TRUE },         /* OBM_ZOOM */
111     { obm_reduce, TRUE },       /* OBM_REDUCE */
112     { obm_lfarrow, TRUE },      /* OBM_LFARROW */
113     { obm_rgarrow, TRUE },      /* OBM_RGARROW */
114     { obm_dnarrow, TRUE },      /* OBM_DNARROW */
115     { obm_uparrow, TRUE },      /* OBM_UPARROW */
116     { obm_close, TRUE },        /* OBM_CLOSE */
117     { obm_old_restore, FALSE }, /* OBM_OLD_RESTORE */
118     { obm_old_zoom, FALSE },    /* OBM_OLD_ZOOM */
119     { obm_old_reduce, FALSE },  /* OBM_OLD_REDUCE */
120     { obm_btncorners, FALSE },  /* OBM_BTNCORNERS */
121     { obm_checkboxes, FALSE },  /* OBM_CHECKBOXES */
122     { obm_check, FALSE },       /* OBM_CHECK */
123     { obm_btsize, FALSE },      /* OBM_BTSIZE */
124     { obm_old_lfarrow, FALSE }, /* OBM_OLD_LFARROW */
125     { obm_old_rgarrow, FALSE }, /* OBM_OLD_RGARROW */
126     { obm_old_dnarrow, FALSE }, /* OBM_OLD_DNARROW */
127     { obm_old_uparrow, FALSE }, /* OBM_OLD_UPARROW */
128     { obm_size, FALSE },        /* OBM_SIZE */
129     { obm_old_close, FALSE },   /* OBM_OLD_CLOSE */
130 };
131
132
133   /* All the colors used in the xpm files must be included in this   */
134   /* list, to make sure that the loaded bitmaps only use colors from */
135   /* the Windows colormap. Note: the PALETTEINDEX() are not really   */
136   /* palette indexes, but system colors that will be converted to    */
137   /* indexes later on.                                               */
138
139 #ifndef HAVE_LIBXXPM
140 typedef struct {
141     char  *name;
142     char  *value;
143     Pixel  pixel;
144 } XpmColorSymbol;
145 #endif /* !defined(HAVE_LIBXXPM) */
146
147 static XpmColorSymbol OBM_Colors[] =
148 {
149     { "black",            NULL, (Pixel)RGB(0,0,0) },
150     { "white",            NULL, (Pixel)RGB(255,255,255) },
151     { "red",              NULL, (Pixel)RGB(255,0,0) },
152     { "green",            NULL, (Pixel)RGB(0,255,0) },
153     { "blue",             NULL, (Pixel)RGB(0,0,255) },
154     { "yellow",           NULL, (Pixel)RGB(255,255,0) },
155     { "cyan",             NULL, (Pixel)RGB(0,255,255) },    
156     { "dkyellow",         NULL, (Pixel)RGB(128,128,0) },
157     { "purple",           NULL, (Pixel)RGB(128,0,128) },
158     { "ltgray",           NULL, (Pixel)RGB(192,192,192) },
159     { "dkgray",           NULL, (Pixel)RGB(128,128,128) },
160     { "button_face",      NULL, (Pixel)PALETTEINDEX(COLOR_BTNFACE) },
161     { "button_shadow",    NULL, (Pixel)PALETTEINDEX(COLOR_BTNSHADOW) },
162     { "button_highlight", NULL, (Pixel)PALETTEINDEX(COLOR_BTNHIGHLIGHT) },
163     { "button_edge",      NULL, (Pixel)PALETTEINDEX(COLOR_BTNHIGHLIGHT) },
164     { "button_text",      NULL, (Pixel)PALETTEINDEX(COLOR_BTNTEXT) },
165     { "window_frame",     NULL, (Pixel)PALETTEINDEX(COLOR_WINDOWFRAME) }
166 };
167
168 #define NB_COLOR_SYMBOLS (sizeof(OBM_Colors)/sizeof(OBM_Colors[0]))
169
170   /* These are the symbolic colors for monochrome bitmaps   */
171   /* This is needed to make sure that black is always 0 and */
172   /* white always 1, as required by Windows.                */
173
174 #ifdef HAVE_LIBXXPM
175 static XpmColorSymbol OBM_BlackAndWhite[2] =
176 {
177     { "black", NULL, 0 },
178     { "white", NULL, 0xffffffff }
179 };
180 #endif /* defined(HAVE_LIBXXPM) */
181
182 extern const DC_FUNCTIONS *X11DRV_DC_Funcs;  /* hack */
183
184
185 /***********************************************************************
186  *           OBM_InitColorSymbols
187  */
188 static BOOL OBM_InitColorSymbols()
189 {
190     static BOOL initialized = FALSE;
191     int i;
192
193     if (initialized) return TRUE;  /* Already initialised */
194     initialized = TRUE;
195
196     for (i = 0; i < NB_COLOR_SYMBOLS; i++)
197     {
198         if (OBM_Colors[i].pixel & 0xff000000)  /* PALETTEINDEX */
199             OBM_Colors[i].pixel = X11DRV_PALETTE_ToPhysical( NULL,
200                                     GetSysColor(OBM_Colors[i].pixel & 0xff));
201         else  /* RGB*/
202             OBM_Colors[i].pixel = X11DRV_PALETTE_ToPhysical( NULL, OBM_Colors[i].pixel);
203     }
204     return TRUE;
205 }
206
207
208 /***********************************************************************
209  *           OBM_MakeBitmap
210  *
211  * Allocate a GDI bitmap.
212  */
213 #ifdef HAVE_LIBXXPM
214 static HBITMAP16 OBM_MakeBitmap( WORD width, WORD height,
215                                  WORD bpp, Pixmap pixmap )
216 {
217     HBITMAP hbitmap;
218     BITMAPOBJ * bmpObjPtr;
219
220     if (!pixmap) return 0;
221
222     if (!(bmpObjPtr = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC, &hbitmap ))) return 0;
223     bmpObjPtr->size.cx = width;
224     bmpObjPtr->size.cy = height;
225     bmpObjPtr->bitmap.bmType       = 0;
226     bmpObjPtr->bitmap.bmWidth      = width;
227     bmpObjPtr->bitmap.bmHeight     = height;
228     bmpObjPtr->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
229     bmpObjPtr->bitmap.bmPlanes     = 1;
230     bmpObjPtr->bitmap.bmBitsPixel  = bpp;
231     bmpObjPtr->bitmap.bmBits       = NULL;
232     bmpObjPtr->dib                 = NULL;
233
234     bmpObjPtr->funcs = X11DRV_DC_Funcs;
235     bmpObjPtr->physBitmap = (void *)pixmap;
236
237     GDI_ReleaseObj( hbitmap );
238     return hbitmap;
239 }
240 #endif /* defined(HAVE_LIBXXPM) */
241
242
243 /***********************************************************************
244  *           OBM_CreateBitmaps
245  *
246  * Create the 2 bitmaps from XPM data.
247  */
248 static BOOL OBM_CreateBitmaps( char **data, BOOL color,
249                                HBITMAP16 *bitmap, HBITMAP16 *mask, POINT *hotspot )
250 {
251 #ifdef HAVE_LIBXXPM
252     XpmAttributes *attrs;
253     Pixmap pixmap, pixmask;
254     int err;
255
256     attrs = (XpmAttributes *)HeapAlloc( GetProcessHeap(), 0, XpmAttributesSize() );
257     if (attrs == NULL) return FALSE;
258     attrs->valuemask    = XpmColormap | XpmDepth | XpmColorSymbols | XpmHotspot;
259     attrs->colormap     = X11DRV_PALETTE_PaletteXColormap;
260     attrs->depth        = color ? screen_depth : 1;
261     attrs->colorsymbols = (attrs->depth > 1) ? OBM_Colors : OBM_BlackAndWhite;
262     attrs->numsymbols   = (attrs->depth > 1) ? NB_COLOR_SYMBOLS : 2;
263
264     err = TSXpmCreatePixmapFromData( gdi_display, root_window, data, &pixmap, &pixmask, attrs );
265     if (err != XpmSuccess)
266     {
267         HeapFree( GetProcessHeap(), 0, attrs );
268         return FALSE;
269     }
270
271     if (hotspot)
272     {
273         hotspot->x = attrs->x_hotspot;
274         hotspot->y = attrs->y_hotspot;
275     }
276
277     if (bitmap)
278         *bitmap = OBM_MakeBitmap( attrs->width, attrs->height,
279                                   attrs->depth, pixmap );
280         
281     if (mask)
282         *mask = OBM_MakeBitmap( attrs->width, attrs->height,
283                                 1, pixmask );
284
285     HeapFree( GetProcessHeap(), 0, attrs );
286
287     if (pixmap && (!bitmap || !*bitmap)) TSXFreePixmap( gdi_display, pixmap );
288     if (pixmask && (!mask || !*mask)) TSXFreePixmap( gdi_display, pixmask );
289
290     if (bitmap && !*bitmap)
291     {
292         if (mask && *mask) DeleteObject( *mask );
293         return FALSE;
294     }
295     return TRUE;
296 #else /* defined(HAVE_LIBXXPM) */
297     FIXME_(x11drv)(
298         "Xpm support not in the binary, "
299         "please install the Xpm and Xpm-devel packages and recompile wine\n"
300     );
301     return FALSE;
302 #endif /* defined(HAVE_LIBXXPM) */
303 }
304
305
306 /***********************************************************************
307  *           OBM_LoadBitmap
308  */
309 static HBITMAP16 OBM_LoadBitmap( WORD id )
310 {
311     HBITMAP16 bitmap;
312
313     if ((id < OBM_FIRST) || (id > OBM_LAST)) return 0;
314     id -= OBM_FIRST;
315     if (!OBM_Pixmaps_Data[id].data) return 0;
316
317     if (!OBM_InitColorSymbols()) return 0;
318
319     if (!OBM_CreateBitmaps( OBM_Pixmaps_Data[id].data, OBM_Pixmaps_Data[id].color,
320                             &bitmap, NULL, NULL ))
321     {
322         WARN_(bitmap)("Error creating OEM bitmap %d\n", OBM_FIRST+id );
323         return 0;
324     }
325     return bitmap;
326 }
327
328
329 /***********************************************************************
330  *           LoadOEMResource (X11DRV.@)
331  *
332  */
333 HANDLE X11DRV_LoadOEMResource(WORD resid, WORD type)
334 {
335     switch(type) {
336     case OEM_BITMAP:
337         return OBM_LoadBitmap(resid);
338
339     default:
340         ERR_(x11drv)("Unknown type\n");
341     }
342     return 0;
343 }
344
345
346 /***********************************************************************
347  *           X11DRV_OBM_Init
348  *
349  * Initializes the OBM_Pixmaps_Data and OBM_Icons_Data struct
350  */
351 BOOL X11DRV_OBM_Init(void)
352 {
353     if (TWEAK_WineLook == WIN31_LOOK) {
354         OBM_Pixmaps_Data[OBM_ZOOMD - OBM_FIRST].data = obm_zoomd;
355         OBM_Pixmaps_Data[OBM_REDUCED - OBM_FIRST].data = obm_reduced;
356         OBM_Pixmaps_Data[OBM_ZOOM - OBM_FIRST].data = obm_zoom;
357         OBM_Pixmaps_Data[OBM_REDUCE - OBM_FIRST].data = obm_reduce;
358         OBM_Pixmaps_Data[OBM_CLOSE - OBM_FIRST].data = obm_close;
359         OBM_Pixmaps_Data[OBM_RESTORE - OBM_FIRST].data = obm_restore;
360         OBM_Pixmaps_Data[OBM_RESTORED - OBM_FIRST].data = obm_restored;
361     }
362     else {
363         OBM_Pixmaps_Data[OBM_ZOOMD - OBM_FIRST].data = obm_zoomd_95;
364         OBM_Pixmaps_Data[OBM_REDUCED - OBM_FIRST].data = obm_reduced_95;
365         OBM_Pixmaps_Data[OBM_ZOOM - OBM_FIRST].data = obm_zoom_95;
366         OBM_Pixmaps_Data[OBM_REDUCE - OBM_FIRST].data = obm_reduce_95;
367         OBM_Pixmaps_Data[OBM_CLOSE - OBM_FIRST].data = obm_close_95;
368         OBM_Pixmaps_Data[OBM_RESTORE - OBM_FIRST].data = obm_restore_95;
369         OBM_Pixmaps_Data[OBM_RESTORED - OBM_FIRST].data = obm_restored_95;
370     }
371
372     return 1;
373 }
374