wmbiff: Fix compiler warnings from deprecated gnutls types.
[dockapps.git] / wmbiff / wmbiff / MessageList.c
blob3cda108a606fa6e2602a188b555e6a5673abe3cd
1 #include "Client.h"
2 #include "MessageList.h"
3 #include <X11/Xlib.h>
4 #ifdef HAVE_X11_XPM_H
5 #include <X11/xpm.h>
6 #endif
7 #ifdef HAVE_XPM_H
8 #include <xpm.h>
9 #endif
10 #include <X11/Xutil.h> /* needed for Region on solaris? */
11 #include <assert.h>
13 #define LEFT_MAR 6
14 #define RIGHT_MAR 6
15 #define COL_SEP 4
17 extern Display *display;
18 extern Window Root;
19 extern int screen;
20 extern int x_fd;
21 extern int d_depth;
22 extern Window win;
24 static XSizeHints mysizehints;
25 extern Pixel back_pix, fore_pix;
26 static Window newwin;
27 static GC localGC;
28 extern Pixel GetColor(const char *name);
30 static XFontStruct *fn;
31 static int fontHeight;
32 extern const char *foreground;
33 extern const char *background;
35 Pop3 Active_pc;
37 static int loadFont(const char *fontname)
39 if (display != NULL) {
40 fn = XLoadQueryFont(display, fontname);
41 if (fn) {
42 XSetFont(display, localGC, fn->fid);
43 fontHeight =
44 fn->max_bounds.ascent + fn->max_bounds.descent + 2;
45 return 0;
46 } else {
47 printf("couldn't set font! (%s)\n", fontname);
51 return -1;
54 static int flush_expose(Window w)
56 XEvent dummy;
57 int i = 0;
59 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
60 i++;
62 return i;
65 struct msglst *Headers;
66 void msglst_show(Pop3 pc, int x, int y)
68 int maxfrm = 0;
69 int maxsubj = 0;
70 int limit = 10;
71 XGCValues gcv;
72 unsigned long gcm;
74 Active_pc = pc; /* hold so we can release later. */
76 /* local gc */
77 gcm = GCForeground | GCBackground | GCGraphicsExposures;
78 gcv.foreground = GetColor(foreground);
79 gcv.background = GetColor(background);
80 gcv.graphics_exposures = 0;
81 localGC = XCreateGC(display, Root, gcm, &gcv);
83 if (fn == NULL) {
84 /* loadFont? or use a proportional instead? mmm. */
85 if (loadFont("-*-fixed-*-r-*-*-10-*-*-*-*-*-*-*") < 0) {
86 return;
89 if (pc->getHeaders == NULL) {
90 DM(pc, DEBUG_INFO, "no getHeaders callback\n");
91 return;
93 Headers = pc->getHeaders(pc);
94 if (Headers == NULL) {
95 #define NO_MSG "no new messages"
96 mysizehints.height = 5 + fontHeight;
97 mysizehints.width = XTextWidth(fn, NO_MSG, strlen(NO_MSG));
98 DM(pc, DEBUG_INFO, "no new messages\n");
99 } else {
100 struct msglst *h;
101 mysizehints.height = 5;
102 for (h = Headers; h != NULL && limit > 0; h = h->next, limit--) {
103 int frmlen;
104 char *c;
105 int subjlen;
107 if ((c = index(h->from, '\r')) != NULL) {
108 *c = '\0'; /* chomp newlines */
110 if ((c = index(h->subj, '\r')) != NULL) {
111 *c = '\0'; /* chomp newlines */
114 if ((c = index(h->from, '<')) != NULL) {
115 *c = '\0'; /* chomp <foo@bar */
117 if (h->from[0] == '"') { /* remove "'s */
118 for (c = &h->from[1]; *c && *c != '"'; c++) {
119 *(c - 1) = *c;
121 *(c - 1) = '\0';
125 subjlen = XTextWidth(fn, h->subj, strlen(h->subj));
126 frmlen = XTextWidth(fn, h->from, strlen(h->from));
127 if (frmlen > maxfrm) {
128 maxfrm = frmlen;
130 if (subjlen > maxsubj) {
131 maxsubj = subjlen;
133 mysizehints.height += fontHeight;
135 mysizehints.width =
136 maxfrm + maxsubj + LEFT_MAR + RIGHT_MAR + COL_SEP;
139 /* Create a window to hold the stuff */
140 mysizehints.flags = USSize | USPosition;
141 mysizehints.x = max(x - mysizehints.width, 0);
142 mysizehints.y = max(y - mysizehints.height, 0);
144 newwin =
145 XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
146 mysizehints.width, mysizehints.height, 2,
147 gcv.foreground, gcv.background);
148 XSetWMNormalHints(display, newwin, &mysizehints);
149 XStoreName(display, newwin, pc->label);
150 XSelectInput(display, newwin, ExposureMask);
152 { /* I confess I don't know what this does or whether it matters */
153 XSetWindowAttributes xswa;
154 xswa.backing_store = Always;
155 xswa.bit_gravity = CenterGravity;
156 XChangeWindowAttributes(display, newwin,
157 CWBackingStore | CWBitGravity, &xswa);
160 XMapWindow(display, newwin);
163 /* may be called without the window open */
164 void msglst_hide(void)
166 if (newwin) {
167 flush_expose(newwin); /* swallow the messages */
168 XDestroyWindow(display, newwin);
169 // } else {
170 // no window fprintf(stderr, "unexpected error destroying msglist window\n");
171 if (Active_pc->releaseHeaders != NULL && Headers != NULL) {
172 Active_pc->releaseHeaders(Active_pc, Headers);
174 newwin = 0;
178 void msglst_redraw(void)
180 XEvent dummy;
181 unsigned int width, height;
182 unsigned int bw, d;
183 int x, y;
184 Window r;
186 if (newwin == 0) {
187 return;
190 while (XCheckTypedWindowEvent(display, newwin, Expose, &dummy));
191 XGetGeometry(display, newwin, &r, &x, &y, &width, &height, &bw, &d);
193 XSetForeground(display, localGC, GetColor(background));
194 XFillRectangle(display, newwin, localGC, 0, 0, width, height);
196 XSetForeground(display, localGC, GetColor(foreground));
197 XSetBackground(display, localGC, GetColor(background));
199 if (Headers == NULL) {
200 XDrawString(display, newwin, localGC, 0, fontHeight,
201 NO_MSG, strlen(NO_MSG));
202 flush_expose(newwin);
203 } else {
204 int linenum;
205 struct msglst *h;
206 int limit = 10;
207 int maxfrm = 0;
209 /* draw the from lines */
210 for (h = Headers, linenum = 0; h != NULL && linenum < limit;
211 h = h->next, linenum++) {
212 int frm = XTextWidth(fn, h->from, strlen(h->from));
213 if (frm > maxfrm) {
214 maxfrm = frm;
216 XDrawString(display, newwin, localGC, LEFT_MAR,
217 (linenum + 1) * fontHeight, h->from,
218 strlen(h->from));
221 /* draw the subject lines */
222 for (h = Headers, linenum = 0; h != NULL && linenum < limit;
223 h = h->next, linenum++) {
224 XDrawString(display, newwin, localGC,
225 LEFT_MAR + maxfrm + COL_SEP,
226 (linenum + 1) * fontHeight, h->subj,
227 strlen(h->subj));