Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 3a9bb55684
Fetching contributors…

Cannot retrieve contributors at this time

file 407 lines (346 sloc) 9.961 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
/* xmisc.c */

/* Copyright 1997 by Steve Kirkendall */

char id_xmisc[] = "$Id: xmisc.c,v 2.9 1997/11/12 02:10:36 steve Exp $";

#include "elvis.h"
#ifdef GUI_X11
# include "guix11.h"


static X_LOADEDFONT *fonts; /* list of allocated fonts */
static X_LOADEDCOLOR *colors; /* list of allocated colors */


/* This function loads a font if it isn't already loaded. */
X_LOADEDFONT *x_loadfont(name)
char *name; /* name of the font */
{
X_LOADEDFONT *font;
XFontStruct *info;

/* see if it is already loaded */
for (font = fonts; font && strcmp(name, font->name); font = font->next)
{
}

/* if already loaded, then just increment its count */
if (font)
{
font->links++;
return font;
}

/* else load the font into a new stucture */
info = XLoadQueryFont(x_display, name);
if (!info)
{
msg(MSG_ERROR, "[s]can't load font $1", name);
return NULL;
}
font = (X_LOADEDFONT *)safekept(1, sizeof(X_LOADEDFONT));
font->fontinfo = info;
font->name = safekdup(name);
font->height = info->descent + info->ascent;
font->links = 1;

/* link the new structure into the list, and return it */
font->next = fonts;
fonts = font;
return font;
}


/* This function unloads a font. If no window is using the font after that,
* then the storage space is freed.
*/
void x_unloadfont(font)
X_LOADEDFONT *font; /* font to be freed */
{
X_LOADEDFONT *scan;

/* If no font was given, do nothing */
if (!font)
{
return;
}

assert(fonts != NULL);

/* decrement the count. If other windows are still using this
* font, then that's all we should for now.
*/
if (--font->links > 0)
{
return;
}

/* delete the font from the list of fonts */
if (fonts == font)
{
fonts = font->next;
}
else
{
for (scan = fonts; scan->next != font; scan = scan->next)
{
assert(scan->next);
}
scan->next = font->next;
}

/* free its resources */
safefree(font->name);
XFreeFont(x_display, font->fontinfo);
safefree(font);
}


/* This function allocates a color */
unsigned long x_loadcolor(name, def)
CHAR *name; /* name of color to load */
unsigned long def; /* default color, if can't load named color */
{
XColor exact, color;
X_LOADEDCOLOR *scan;

/* if mono, or no name, then just use the default */
if (x_mono || !name || !*name)
goto UseDefault;

/* was this color name used before? */
for (scan = colors; scan; scan = scan->next)
{
if (!CHARcmp(name, scan->name))
{
scan->links++;
return scan->pixel;
}
}

/* try to load the color */
if (!XAllocNamedColor(x_display, x_colormap, tochar8(name), &exact, &color))
{
msg(MSG_WARNING, "[S]could not allocate color $1", name);
goto UseDefault;
}

/* was it rounded to a previous color? */
for (scan = colors; scan; scan = scan->next)
{
if (scan->pixel == color.pixel)
{
scan->links++;
return scan->pixel;
}
}

/* new color -- remember it */
scan = safekept(1, sizeof(*scan));
scan->name = CHARkdup(name);
scan->pixel = color.pixel;
scan->links = 1;
scan->next = colors;
colors = scan;
return color.pixel;

UseDefault:
/* look for the default pixel code in the list */
for (scan = colors; scan; scan = scan->next)
{
if (scan->pixel == def)
{
scan->links++;
return def;
}
}

/* not in list -- add it */
scan = safekept(1, sizeof *scan);
scan->name = toCHAR(def == x_black ? "black" : "white");
scan->pixel = def;
scan->links = 1;
scan->next = colors;
colors = scan;
return def;
}


void x_unloadcolor(pixel)
unsigned long pixel; /* a color to free */
{
X_LOADEDCOLOR *scan, *lag;

/* never load/unload the default white & black values */
if (pixel == x_white || pixel == x_black)
{
return;
}

/* find the color */
for (lag = NULL, scan = colors; scan->pixel != pixel; lag = scan, scan = scan->next)
{
assert(scan->next);
}

/* decrement the link counter. If other windows are using it,
* then leave it.
*/
if (--scan->links > 0)
{
return;
}

/* free it */
XFreeColors(x_display, x_colormap, &pixel, 1, 0);
safefree(scan->name);
if (lag)
lag->next = scan->next;
else
colors = scan->next;
safefree(scan);
}


/* draw a beveled button. It can be an arrow head, or a rectangular button.
* If height=0, the area will be cleared to the window's background color.
* Positive heights make the button "stick out", and negative values make it
* appear to be "pushed in"; either way, the button's foreground will be
* drawn in the scrollbar foreground color (regardless of the window's normal
* foreground color).
*/
void x_drawbevel(xw, win, x, y, w, h, dir, height)
X11WIN *xw; /* top-level app window to draw in */
Window win; /* widget where drawing should occur */
int x, y; /* upper-left corner of the rectangle */
unsigned w, h; /* width and height of the rectangle */
_char_ dir; /* one of "udrl" for arrow, or 'b' for button */
int height; /* height of button, 0 to disable */
{
XGCValues gcv;
int b, r; /* bottom & right edges */
unsigned long topleft;
unsigned long bottomright;
XPoint vertex[4]; /* points of arrowhead */
int dx[4], dy[4]; /* inward movement */
int ntop; /* number of points in topleft color */
int i;

/* if height is 0, then clear the button area */
if (height == 0)
{
XClearArea(x_display, win, x, y, w, h, False);
return;
}

/* choose the edge colors */
if (height > 0)
{
topleft = x_white;
bottomright = x_black;
}
else
{
topleft = x_black;
bottomright = x_white;
height = -height;
}

/* set the foreground color to the scrollbar's color */
gcv.foreground = (win == xw->sb.win ? xw->sb.fgscroll : xw->tb.face);
gcv.fill_style = FillSolid;
XChangeGC(x_display, xw->gc, GCForeground|GCFillStyle, &gcv);
xw->fg = gcv.foreground;

/* For arrow heads, choose the vertex points. For rectangular,
* draw the button and then return.
*/
switch (dir)
{
case 'u':
vertex[0].x = 0, vertex[0].y = h - 1, dx[0] = 1, dy[0] = -1;
vertex[1].x = w / 2, vertex[1].y = 0, dx[1] = 0, dy[1] = 1;
vertex[2].x = w - 1, vertex[2].y = h - 1, dx[2] = dy[2] = -1;
ntop = 1;
break;

case 'd':
vertex[0].x = w / 2, vertex[0].y = h - 1, dx[0] = 0, dy[0] = -1;
vertex[1].x = 0, vertex[1].y = 0, dx[1] = dy[1] = 1;
vertex[2].x = w - 1, vertex[2].y = 0, dx[2] = -1, dy[2] = 1;
ntop = 2;
break;

case 'l':
vertex[0].x = 0, vertex[0].y = h / 2, dx[0] = 1, dy[0] = 0;
vertex[1].x = w - 1, vertex[1].y = 0, dx[1] = -1, dy[1] = 1;
vertex[2].x = w - 1, vertex[2].y = h - 1, dx[2] = dy[2] = -1;
ntop = 1;
break;

case 'r':
vertex[0].x = 0, vertex[0].y = h - 1, dx[0] = 1, dy[0] = -1;
vertex[1].x = 0, vertex[1].y = 0, dx[1] = dy[1] = 1;
vertex[2].x = w - 1, vertex[2].y = h / 2, dx[2] = -1, dy[2] = 0;
ntop = 2;
break;

default: /* rectangular button */
/* draw the button's face */
XFillRectangle(x_display, win, xw->gc, x, y, w, h);

/* locate the bottom & right edges */
b = y + h - 1;
r = x + w - 1;

/* draw the outermost bevel sides */
while (--height >= 0)
{
/* draw the bottom bevel edges */
XSetForeground(x_display, xw->gc, bottomright);
XDrawLine(x_display, win, xw->gc, x, b, r, b);

/* draw the top & left bevel edges */
XSetForeground(x_display, xw->gc, topleft);
XDrawLine(x_display, win, xw->gc, x, y, r, y);
XDrawLine(x_display, win, xw->gc, x, y, x, b);

/* draw the right bevel edge */
XSetForeground(x_display, xw->gc, bottomright);
xw->fg = bottomright;
XDrawLine(x_display, win, xw->gc, r, y, r, b);

/* move the edges in slightly, to continue the bevel */
x++;
y++;
r--;
b--;
}

/* for rectangular buttons, we're done */
return;
}

/* If we get here, then we're drawing a triangular button */

/* Translate the polygon to its intended position within the window */
for (i = 0; i < 3; i++)
{
vertex[i].x += x;
vertex[i].y += y;
}

/* Close the polygon. This isn't needed for XFillPolygon(), but it
* helps when drawing the edges via XDrawLines().
*/
vertex[3] = vertex[0], dx[3] = dx[0], dy[3] = dy[0];

/* draw the face of the button */
XFillPolygon(x_display, win, xw->gc, vertex, 3, Convex, CoordModeOrigin);

/* draw the beveled edges */
while (--height >= 0)
{
XSetForeground(x_display, xw->gc, bottomright);
XDrawLines(x_display, win, xw->gc, vertex + ntop, 4 - ntop, CoordModeOrigin);
XSetForeground(x_display, xw->gc, topleft);
XDrawLines(x_display, win, xw->gc, vertex, ntop + 1, CoordModeOrigin);

/* move the vertices inward for next iteration */
for (i = 0; i < QTY(vertex); i++)
{
vertex[i].x += dx[i];
vertex[i].y += dy[i];
}
}
xw->fg = bottomright;
}

/* Draw some text. For color displays, this just calls XDrawString. For
* mono displays, it draws black characters surrounded by a white outline.
*/
void x_drawstring(display, win, gc, x, y, str, len)
Display *display;
Window win;
GC gc;
int x, y;
char *str;
int len;
{
int i;
static struct {int dx, dy;} delta[] = {
{-2, -1}, { 2, -1}, {-2, 0}, { 2, 0}, {-2, 1}, { 2, 1},
{-1, -1}, { 1, -1}, {-1, 1}, { 1, 1},
{ 0, -1}, {-1, 0}, { 1, 0}, { 0, 1}};
static int start[] = {QTY(delta), QTY(delta) - 4, QTY(delta) - 8, 0};

if (x_mono && o_outlinemono > 0)
{
XSetForeground(display, gc, x_white);
for (i = start[o_outlinemono]; i < QTY(delta); i++)
{
XDrawString(display, win, gc,
x + delta[i].dx, y + delta[i].dy, str, len);
}
XSetForeground(display, gc, x_black);
}
XDrawString(display, win, gc, x, y, str, len);
}


# ifdef NEED_XRMCOMBINEFILEDATABASE
/* X11R4 lacks this function */
void XrmCombineFileDatabase(filename, target_db, override)
char *filename; /* name of resource file */
XrmDatabase *target_db; /* address of resource database */
Bool override; /* ignored -- always overrides */
{
XrmDatabase *source_db;

source_db = XrmGetFileDatabase(filename);
if (source_db)
XrmMergeDatabases(source_db, target_db);
}
# endif /* NEED_XRMCOMBINEFILEDATABASE */

#endif /* GUI_X11 */
Something went wrong with that request. Please try again.