1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <X11/Xlib.h>
5 #include <X11/Intrinsic.h>
6 #include <X11/xpm.h>
7 #include <Xm/Xm.h>
8 
9 #include "icons.h"
10 #include "misc.h"
11 
12 #include "xpm/cw.xpm"
13 #include "xpm/ccw.xpm"
14 #include "xpm/fliph.xpm"
15 #include "xpm/flipv.xpm"
16 #include "xpm/prev.xpm"
17 #include "xpm/next.xpm"
18 #include "xpm/zoomin.xpm"
19 #include "xpm/zoomout.xpm"
20 #include "xpm/exit.xpm"
21 
22 #include "xpm/question.xpm"
23 #include "xpm/dir.xpm"
24 #include "xpm/file.xpm"
25 #include "xpm/unknown.xpm"
26 
patch_bg(XImage * image,XImage * shape,int width,int height,Pixel bg)27 static void patch_bg(XImage *image, XImage *shape,
28 		     int width, int height, Pixel bg)
29 {
30     unsigned int x,y;
31 
32     for (y = 0; y < height; y++)
33 	for (x = 0; x < width; x++)
34 	    if (!XGetPixel(shape, x, y))
35 		XPutPixel(image, x, y, bg);
36 }
37 
38 static void
add_pixmap(Display * dpy,Pixel bg,char * name,char ** data)39 add_pixmap(Display *dpy, Pixel bg, char *name, char **data)
40 {
41     XImage *image,*shape;
42     XpmAttributes attr;
43     char sname[32];
44 
45     memset(&attr,0,sizeof(attr));
46     XpmCreateImageFromData(dpy,data,&image,&shape,&attr);
47 
48     if (shape) {
49 	patch_bg(image,shape,attr.width,attr.height,bg);
50 	snprintf(sname,sizeof(sname),"%s_shape",name);
51 	XmInstallImage(shape,sname);
52     }
53     XmInstallImage(image,name);
54 }
55 
x11_icon_fit(Display * dpy,Pixmap icon,unsigned long bg,int width,int height)56 Pixmap x11_icon_fit(Display *dpy, Pixmap icon, unsigned long bg,
57 		    int width, int height)
58 {
59     Pixmap pix;
60     GC gc;
61     XGCValues values;
62     Window root;
63     unsigned int w,h,b,d;
64     int x,y;
65 
66     XGetGeometry(dpy,icon,&root,&x,&y,&w,&h,&b,&d);
67     pix = XCreatePixmap(dpy,icon,width,height,d);
68 
69     /* fill background */
70     values.foreground = bg;
71     values.background = bg;
72     values.fill_style = FillSolid;
73     gc = XCreateGC(dpy, icon, GCForeground | GCBackground | GCFillStyle,
74 		   &values);
75     XFillRectangle(dpy,pix,gc,0,0,width,height);
76 
77     /* blit */
78     if (w <= width && h <= height) {
79 	/* just center ... */
80 	x = (width  - w) / 2;
81 	y = (height - h) / 2;
82 	XCopyArea(dpy,icon,pix,gc,0,0,w,h,x,y);
83     } else {
84 	/* must scale down */
85 #if 0
86 	float xs,ys,scale;
87 
88 	xs = (float)width  / w;
89 	ys = (float)height / h;
90 	scale = (xs < ys) ? xs : ys;
91 	w = w * scale;
92 	h = h * scale;
93 	if (0 == w) w = 1;
94 	if (0 == h) h = 1;
95 
96 	x = (width  - w) / 2;
97 	y = (height - h) / 2;
98 	XCopyArea(dpy,icon,pix,gc,0,0,w,h,x,y);
99 #endif
100 	x = (width  - w) / 2;
101 	y = (height - h) / 2;
102 	if (x < 0)
103 	    x = 0;
104 	if (y < 0)
105 	    y = 0;
106 	XCopyArea(dpy,icon,pix,gc,0,0,w,h,0,0);
107     }
108 
109     XFreeGC(dpy, gc);
110     return pix;
111 }
112 
113 void
x11_icons_init(Display * dpy,unsigned long bg)114 x11_icons_init(Display *dpy, unsigned long bg)
115 {
116     add_pixmap(dpy, bg, "rotcw",    cw_xpm);
117     add_pixmap(dpy, bg, "rotccw",   ccw_xpm);
118     add_pixmap(dpy, bg, "fliph",    fliph_xpm);
119     add_pixmap(dpy, bg, "flipv",    flipv_xpm);
120     add_pixmap(dpy, bg, "prev",     prev_xpm);
121     add_pixmap(dpy, bg, "next",     next_xpm);
122     add_pixmap(dpy, bg, "zoomin",   zoomin_xpm);
123     add_pixmap(dpy, bg, "zoomout",  zoomout_xpm);
124     add_pixmap(dpy, bg, "exit",     exit_xpm);
125 
126     add_pixmap(dpy, bg, "question", question_xpm);
127     add_pixmap(dpy, bg, "dir",      dir_xpm);
128     add_pixmap(dpy, bg, "file",     file_xpm);
129     add_pixmap(dpy, bg, "unknown",  unknown_xpm);
130 }
131