wmii/liblitz/geometry.c

226 lines
5.0 KiB
C
Raw Normal View History

2005-11-18 18:54:58 +03:00
/*
2006-01-20 17:20:24 +03:00
* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
2005-11-18 18:54:58 +03:00
* See LICENSE file for license details.
*/
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "blitz.h"
2006-01-27 19:44:05 +03:00
int blitz_strtoalign(Align *result, char *val)
2005-11-18 18:54:58 +03:00
{
/*
* note, resize allows syntax like "east-20", this we cannot do
* include zero termination in strncmp checking!
*/
*result = CENTER;
if (!strncmp(val, "west", 4))
*result = WEST;
else if (!strncmp(val, "nwest", 5))
*result = NWEST;
else if (!strncmp(val, "north", 5))
*result = NORTH;
else if (!strncmp(val, "neast", 5))
*result = NEAST;
else if (!strncmp(val, "east", 4))
*result = EAST;
else if (!strncmp(val, "seast", 5))
*result = SEAST;
else if (!strncmp(val, "south", 5))
*result = SOUTH;
else if (!strncmp(val, "swest", 5))
*result = SWEST;
else if (!strncmp(val, "center", 6))
*result = CENTER;
else
return -1;
return 0;
2005-11-18 18:54:58 +03:00
}
/**
* Basic Syntax: <x>,<y>,<width>,<height>
* Each component can be of following format:
* <...> = [+|-]0..n|<alignment>[[+|-]0..n]
*/
int blitz_strtorect(XRectangle *root, XRectangle *r, char *val)
2005-11-18 18:54:58 +03:00
{
const char *err;
2005-12-05 01:45:59 +03:00
char buf[64];
char *x, *y, *w, *h;
char *p;
int rx, ry, rw, rh, sx, sy, sw, sh;
2005-11-18 18:54:58 +03:00
if (!val)
return -1;
2005-12-21 17:03:57 +03:00
rx = r->x;
ry = r->y;
rw = r->width;
rh = r->height;
2005-11-18 18:54:58 +03:00
sx = sy = sw = sh = 0;
x = y = w = h = 0;
cext_strlcpy(buf, val, sizeof(buf));
2005-11-18 18:54:58 +03:00
x = strtok_r(buf, " ", &p);
2005-11-18 18:54:58 +03:00
if (x) {
y = strtok_r(0, " ", &p);
2005-11-18 18:54:58 +03:00
if (y) {
w = strtok_r(0, " ", &p);
2005-11-18 18:54:58 +03:00
if (w) {
h = strtok_r(0, "", &p);
}
}
}
if (x && (sx = (x[0] >= '0') && (x[0] <= '9')))
rx = cext_strtonum(x, 0, 65535, &err);
2005-11-18 18:54:58 +03:00
if (y && (sy = (y[0] >= '0') && (y[0] <= '9')))
ry = cext_strtonum(y, 0, 65535, &err);
2005-11-18 18:54:58 +03:00
if (w && (sw = (w[0] >= '0') && (w[0] <= '9')))
rw = cext_strtonum(w, 0, 65535, &err);
2005-11-18 18:54:58 +03:00
if (h && (sh = (h[0] >= '0') && (h[0] <= '9')))
rh = cext_strtonum(h, 0, 65535, &err);
2005-11-18 18:54:58 +03:00
if (!sx && !sw && x && w
2005-12-05 01:45:59 +03:00
&& x[0] != '-' && x[0] != '+' && w[0] != '-' && w[0] != '+') {
Align ax, aw;
2006-01-27 19:44:05 +03:00
blitz_strtoalign(&ax, x);
blitz_strtoalign(&aw, w);
2005-11-18 18:54:58 +03:00
if ((ax == CENTER) && (aw == EAST)) {
rx = root->x + root->width / 2;
rw = root->width / 2;
2005-11-18 18:54:58 +03:00
} else {
rx = root->x;
2005-11-18 18:54:58 +03:00
if (aw == CENTER) {
rw = root->width / 2;
2005-11-18 18:54:58 +03:00
} else {
rw = root->width;
2005-11-18 18:54:58 +03:00
}
}
} else if (!sx && x && x[0] != '-' && x[0] != '+') {
2005-12-05 01:45:59 +03:00
Align ax;
2006-01-27 19:44:05 +03:00
blitz_strtoalign(&ax, x);
2005-11-18 18:54:58 +03:00
if (ax == CENTER) {
rx = root->x + (root->width / 2) - (rw / 2);
2005-11-18 18:54:58 +03:00
} else if (ax == EAST) {
rx = root->x + root->width - rw;
2005-11-18 18:54:58 +03:00
} else {
rx = root->x;
2005-11-18 18:54:58 +03:00
}
} else if (!sw && w && w[0] != '-' && w[0] != '+') {
2005-12-05 01:45:59 +03:00
Align aw;
2006-01-27 19:44:05 +03:00
blitz_strtoalign(&aw, w);
2005-11-18 18:54:58 +03:00
if (aw == CENTER) {
rw = (root->width / 2) - rx;
2005-11-18 18:54:58 +03:00
} else {
rw = root->width - rx;
2005-11-18 18:54:58 +03:00
}
}
if (!sy && !sh && y && h
2005-12-05 01:45:59 +03:00
&& y[0] != '-' && y[0] != '+' && h[0] != '-' && h[0] != '+') {
Align ay, ah;
2006-01-27 19:44:05 +03:00
blitz_strtoalign(&ay, y);
blitz_strtoalign(&ah, h);
2005-11-18 18:54:58 +03:00
if ((ay == CENTER) && (ah == SOUTH)) {
ry = root->y + root->height / 2;
rh = root->height / 2;
2005-11-18 18:54:58 +03:00
} else {
ry = root->y;
2005-11-18 18:54:58 +03:00
if (ah == CENTER) {
rh = root->height / 2;
2005-11-18 18:54:58 +03:00
} else {
rh = root->height;
2005-11-18 18:54:58 +03:00
}
}
} else if (!sy && y && y[0] != '-' && y[0] != '+') {
2005-12-05 01:45:59 +03:00
Align ay;
2006-01-27 19:44:05 +03:00
blitz_strtoalign(&ay, y);
2005-11-18 18:54:58 +03:00
if (ay == CENTER) {
ry = root->y + (root->height / 2) - (rh / 2);
2005-11-18 18:54:58 +03:00
} else if (ay == SOUTH) {
ry = root->y + root->height - rh;
2005-11-18 18:54:58 +03:00
} else {
ry = root->y;
2005-11-18 18:54:58 +03:00
}
} else if (!sh && h && h[0] != '-' && h[0] != '+') {
2005-12-05 01:45:59 +03:00
Align ah;
2006-01-27 19:44:05 +03:00
blitz_strtoalign(&ah, h);
2005-11-18 18:54:58 +03:00
if (ah == CENTER) {
rh = (root->height / 2) - ry;
2005-11-18 18:54:58 +03:00
} else {
rh = root->height - ry;
2005-11-18 18:54:58 +03:00
}
}
/* now do final calculations */
if (x) {
p = strchr(x, '-');
if (p)
rx -= cext_strtonum(++p, 0, 65535, &err);
2005-11-18 18:54:58 +03:00
p = strchr(x, '+');
if (p)
rx += cext_strtonum(++p, 0, 65535, &err);
2005-11-18 18:54:58 +03:00
}
if (y) {
p = strchr(y, '-');
if (p)
ry -= cext_strtonum(++p, 0, 65535, &err);
2005-11-18 18:54:58 +03:00
p = strchr(y, '+');
if (p)
ry += cext_strtonum(++p, 0, 65535, &err);
2005-11-18 18:54:58 +03:00
}
if (w) {
p = strchr(w, '-');
if (p)
rw -= cext_strtonum(++p, 0, 65535, &err);
2005-11-18 18:54:58 +03:00
p = strchr(w, '+');
if (p)
rw += cext_strtonum(++p, 0, 65535, &err);
2005-11-18 18:54:58 +03:00
}
if (h) {
p = strchr(h, '-');
if (p)
rh -= cext_strtonum(++p, 0, 65535, &err);
2005-11-18 18:54:58 +03:00
p = strchr(h, '+');
if (p)
rh += cext_strtonum(++p, 0, 65535, &err);
2005-11-18 18:54:58 +03:00
}
if (rw < 1)
2005-12-21 17:03:57 +03:00
rw = 10;
if (rh < 1)
2005-12-21 17:03:57 +03:00
rh = 10;
r->x = rx;
r->y = ry;
r->width = rw;
r->height = rh;
return 0;
2005-11-18 18:54:58 +03:00
}
Bool blitz_ispointinrect(int x, int y, XRectangle * r)
2005-11-18 18:54:58 +03:00
{
return (x >= r->x) && (x <= r->x + r->width) && (y >= r->y) && (y <= r->y + r->height);
2005-11-18 18:54:58 +03:00
}
2005-12-05 01:45:59 +03:00
int blitz_distance(XRectangle * origin, XRectangle * target)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int ox = origin->x + origin->width / 2;
int oy = origin->y + origin->height / 2;
int tx = target->x + target->width / 2;
int ty = target->y + target->height / 2;
2005-11-18 18:54:58 +03:00
return (int) sqrt((double) (((ox - tx) * (ox - tx)) + ((oy - ty) * (oy - ty))));
2005-11-18 18:54:58 +03:00
}
void blitz_getbasegeometry(unsigned int size, unsigned int *cols, unsigned int *rows)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
float sq, dummy;
2005-11-18 18:54:58 +03:00
sq = sqrt(size);
2005-11-18 18:54:58 +03:00
if (modff(sq, &dummy) < 0.5)
*rows = floor(sq);
else
*rows = ceil(sq);
*cols = ((*rows) * (*rows)) < (size) ? *rows + 1 : *rows;
2005-11-18 18:54:58 +03:00
}