image_bitmap.c revision 0bbfda8a
1/*
2 * Bitmap image handling functions
3 *
4 * These are what's called "XBM", and represented by the Pixmap X data
5 * type (which has noting to do with "XPM" X PixMaps, just to confuse
6 * you).  This is the format used for various builtin images, buttons,
7 * and cursors, and is also the fallback type for any user-specified
8 * images.  Assuming any user has made and specified an XBM since the
9 * 1980's.
10 */
11
12#include "ctwm.h"
13
14#include <X11/Xmu/Drawing.h>
15
16#include "screen.h"
17#include "util.h"
18
19#include "image.h"
20#include "image_bitmap.h"
21#include "image_bitmap_builtin.h"
22
23
24/* Shared with cursor.c for bitmap cursors */
25int  HotX, HotY;
26
27
28static Image *LoadBitmapImage(const char  *name, ColorPair cp);
29static Pixmap FindBitmap(const char *name, unsigned int *widthp,
30                         unsigned int *heightp);
31
32
33/*
34 * External API's
35 */
36
37/* Simple load-by-name */
38Pixmap
39GetBitmap(const char *name)
40{
41	return FindBitmap(name, &JunkWidth, &JunkHeight);
42}
43
44
45/*
46 * Load with FG/BG adjusted to given colorpair
47 */
48Image *
49GetBitmapImage(const char *name, ColorPair cp)
50{
51	/* Non-animated */
52	if(! strchr(name, '%')) {
53		return (LoadBitmapImage(name, cp));
54	}
55
56	/* Animated */
57	return get_image_anim_cp(name, cp, LoadBitmapImage);
58}
59
60
61/*
62 * Internal bits used by the above
63 */
64
65/***********************************************************************
66 *
67 *  Procedure:
68 *      FindBitmap - read in a bitmap file and return size
69 *
70 *  Returned Value:
71 *      the pixmap associated with the bitmap
72 *      widthp  - pointer to width of bitmap
73 *      heightp - pointer to height of bitmap
74 *
75 *  Inputs:
76 *      name    - the filename to read
77 *
78 ***********************************************************************
79 */
80static Pixmap
81FindBitmap(const char *name, unsigned int *widthp,
82           unsigned int *heightp)
83{
84	char *bigname;
85	Pixmap pm;
86
87	if(!name) {
88		return None;
89	}
90
91	/*
92	 * Names of the form :name refer to hardcoded images that are scaled to
93	 * look nice in title buttons.  Eventually, it would be nice to put in a
94	 * menu symbol as well....
95	 */
96	if(name[0] == ':') {
97		return get_builtin_plain_pixmap(name, widthp, heightp);
98	}
99
100	/*
101	 * Generate a full pathname with any special prefix characters (such
102	 * as ~) expanded.
103	 */
104	bigname = ExpandFilename(name);
105	if(!bigname) {
106		/* Something failed bad */
107		return None;
108	}
109
110	/*
111	 * look along bitmapFilePath resource same as toolkit clients
112	 */
113	pm = XmuLocateBitmapFile(ScreenOfDisplay(dpy, Scr->screen), bigname, NULL,
114	                         0, (int *)widthp, (int *)heightp, &HotX, &HotY);
115	if(pm == None && Scr->IconDirectory && bigname[0] != '/') {
116		/*
117		 * Didn't find it.  Attempt to find icon in old IconDirectory
118		 * (now obsolete)
119		 */
120		free(bigname);
121		asprintf(&bigname, "%s/%s", Scr->IconDirectory, name);
122		if(XReadBitmapFile(dpy, Scr->Root, bigname, widthp, heightp, &pm,
123		                   &HotX, &HotY) != BitmapSuccess) {
124			pm = None;
125		}
126	}
127	free(bigname);
128	if((pm == None) && reportfilenotfound) {
129		fprintf(stderr, "%s:  unable to find bitmap \"%s\"\n", ProgramName, name);
130	}
131
132	return pm;
133}
134
135
136static Image *
137LoadBitmapImage(const char *name, ColorPair cp)
138{
139	Image        *image;
140	Pixmap       bm;
141	unsigned int width, height;
142	XGCValues    gcvalues;
143
144	if(Scr->rootGC == (GC) 0) {
145		Scr->rootGC = XCreateGC(dpy, Scr->Root, 0, &gcvalues);
146	}
147	bm = FindBitmap(name, &width, &height);
148	if(bm == None) {
149		return NULL;
150	}
151
152	image = AllocImage();
153	image->pixmap = XCreatePixmap(dpy, Scr->Root, width, height, Scr->d_depth);
154	gcvalues.background = cp.back;
155	gcvalues.foreground = cp.fore;
156	XChangeGC(dpy, Scr->rootGC, GCForeground | GCBackground, &gcvalues);
157	XCopyPlane(dpy, bm, image->pixmap, Scr->rootGC, 0, 0, width, height,
158	           0, 0, (unsigned long) 1);
159	XFreePixmap(dpy, bm);
160	image->width  = width;
161	image->height = height;
162	return image;
163}
164
165