~ctwm/ctwm/trunk

479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
1
/*
479.1.11 by Matthew Fuller
Rename image_xbm to image_bitmap to better match our internal naming,
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
479.1.12 by Matthew Fuller
Tpyo.
6
 * you).  This is the format used for various builtin images, buttons,
479.1.11 by Matthew Fuller
Rename image_xbm to image_bitmap to better match our internal naming,
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.
479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
10
 */
11
12
#include "ctwm.h"
13
14
#include <X11/Xmu/Drawing.h>
15
16
#include "screen.h"
492.2.34 by Matthew Fuller
Cleanup more unnecessary/misplaced #include's.
17
#include "util.h"
479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
18
19
#include "image.h"
479.1.11 by Matthew Fuller
Rename image_xbm to image_bitmap to better match our internal naming,
20
#include "image_bitmap.h"
479.1.13 by Matthew Fuller
Move the creation and lookup of our builtin :bitmaps into a separate
21
#include "image_bitmap_builtin.h"
479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
22
23
481.1.11 by Matthew Fuller
Move the HotX/Y vars over into image_bitmap.[ch], since they're only
24
/* Shared with cursor.c for bitmap cursors */
25
int  HotX, HotY;
26
27
480.1.17 by Matthew Fuller
const-ify all these image names we pass through this process.
28
static Image *LoadBitmapImage(const char  *name, ColorPair cp);
29
static Pixmap FindBitmap(const char *name, unsigned int *widthp,
479.1.29 by Matthew Fuller
Split the rest of the image* function definitions, and make indent.
30
                         unsigned int *heightp);
479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
31
32
480.1.10 by Matthew Fuller
Move the public functions up to the top and add comments.
33
/*
34
 * External API's
35
 */
36
37
/* Simple load-by-name */
38
Pixmap
480.1.17 by Matthew Fuller
const-ify all these image names we pass through this process.
39
GetBitmap(const char *name)
480.1.10 by Matthew Fuller
Move the public functions up to the top and add comments.
40
{
41
	return FindBitmap(name, &JunkWidth, &JunkHeight);
42
}
43
44
45
/*
46
 * Load with FG/BG adjusted to given colorpair
47
 */
48
Image *
480.1.17 by Matthew Fuller
const-ify all these image names we pass through this process.
49
GetBitmapImage(const char *name, ColorPair cp)
480.1.10 by Matthew Fuller
Move the public functions up to the top and add comments.
50
{
480.1.13 by Matthew Fuller
Swap the Bitmap and Xwd image types over to using get_image_anim_cp()
51
	/* Non-animated */
480.1.10 by Matthew Fuller
Move the public functions up to the top and add comments.
52
	if(! strchr(name, '%')) {
53
		return (LoadBitmapImage(name, cp));
54
	}
480.1.13 by Matthew Fuller
Swap the Bitmap and Xwd image types over to using get_image_anim_cp()
55
56
	/* Animated */
57
	return get_image_anim_cp(name, cp, LoadBitmapImage);
480.1.10 by Matthew Fuller
Move the public functions up to the top and add comments.
58
}
59
60
61
/*
62
 * Internal bits used by the above
63
 */
479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
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
 */
479.1.28 by Matthew Fuller
Split these definitions in image_bitmap.c FindBitmap is actually
80
static Pixmap
480.1.17 by Matthew Fuller
const-ify all these image names we pass through this process.
81
FindBitmap(const char *name, unsigned int *widthp,
479.1.29 by Matthew Fuller
Split the rest of the image* function definitions, and make indent.
82
           unsigned int *heightp)
479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
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] == ':') {
479.1.13 by Matthew Fuller
Move the creation and lookup of our builtin :bitmaps into a separate
97
		return get_builtin_plain_pixmap(name, widthp, heightp);
479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
98
	}
99
100
	/*
480.1.8 by Matthew Fuller
Adjust ExpandFilename so that it _always_ returns a newly allocated
101
	 * Generate a full pathname with any special prefix characters (such
102
	 * as ~) expanded.
479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
103
	 */
104
	bigname = ExpandFilename(name);
105
	if(!bigname) {
480.1.8 by Matthew Fuller
Adjust ExpandFilename so that it _always_ returns a newly allocated
106
		/* Something failed bad */
479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
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
		/*
480.1.9 by Matthew Fuller
Slightly tweak and comment the logic in FindBitmap. Use asprintf() in
117
		 * Didn't find it.  Attempt to find icon in old IconDirectory
118
		 * (now obsolete)
479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
119
		 */
480.1.9 by Matthew Fuller
Slightly tweak and comment the logic in FindBitmap. Use asprintf() in
120
		free(bigname);
121
		asprintf(&bigname, "%s/%s", Scr->IconDirectory, name);
479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
122
		if(XReadBitmapFile(dpy, Scr->Root, bigname, widthp, heightp, &pm,
123
		                   &HotX, &HotY) != BitmapSuccess) {
124
			pm = None;
125
		}
126
	}
480.1.8 by Matthew Fuller
Adjust ExpandFilename so that it _always_ returns a newly allocated
127
	free(bigname);
479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
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
479.1.28 by Matthew Fuller
Split these definitions in image_bitmap.c FindBitmap is actually
136
static Image *
480.1.17 by Matthew Fuller
const-ify all these image names we pass through this process.
137
LoadBitmapImage(const char *name, ColorPair cp)
479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
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) {
501.1.9 by Matthew Fuller
Convert pointer values from None -> NULL in image*.
149
		return NULL;
479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
150
	}
151
480.1.19 by Matthew Fuller
Add an AllocImage() to ensure new Image's are initialized properly,
152
	image = AllocImage();
479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
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;
501.1.9 by Matthew Fuller
Convert pointer values from None -> NULL in image*.
162
	return image;
479.1.8 by Matthew Fuller
Pull the XBM-related functions out of util.c into their own image_*
163
}
164