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 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
|
/*
Copyright (C) 1996, 1997 Ulric Eriksson <ulric@edu.stockholm.se>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the Licence, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include "../common/cmalloc.h"
#include "../common/common.h"
#include "xfonts.h" /* from common */
#include <X11/xpm.h>
#include "AnimatorP.h"
extern char *siaghome;
static void plugin_coords(Widget, XtPointer, long *, long *);
#define offset(field) XtOffsetOf(AnimatorRec, animator.field)
static XtResource resources[] = {
{
XtNanimatorCast, /* name */
XtCAnimatorCast, /* class */
XtRPointer, /* type */
sizeof(XtPointer), /* size */
offset(cast), /* offset */
XtRImmediate, /* default_type */
(XtPointer)0 /* default_addr */
}, {
XtNanimatorNow,
XtCAnimatorNow,
XtRInt, /* should be ulong */
sizeof(int),
offset(now),
XtRImmediate,
(XtPointer)0
}, {
XtNanimatorDelta,
XtCAnimatorDelta,
XtRInt,
sizeof(int),
offset(delta),
XtRImmediate,
(XtPointer)100
}, {
XtNanimatorDuration,
XtCAnimatorDuration,
XtRInt,
sizeof(int),
offset(duration),
XtRImmediate,
(XtPointer)1000
}, {
XtNanimatorMode,
XtCAnimatorMode,
XtRInt,
sizeof(int),
offset(newmode),
XtRImmediate,
(XtPointer)ANI_STOP
}, {
XtNanimatorBgPixmap,
XtCAnimatorBgPixmap,
XtRString,
sizeof(String),
offset(bg_pixmap),
XtRString,
(XtPointer)0
}, {
XtNanimatorPluginCoords,
XtCAnimatorPluginCoords,
XtRPointer,
sizeof(XtPointer),
offset(plugin_coords),
XtRImmediate,
(XtPointer)plugin_coords
}
};
#undef offset
/* methods */
static void DoLayout();
static void Resize();
static XtGeometryResult GeometryManager();
static void ChangeManaged();
static void Redisplay(Widget, XEvent *, Region);
static void Realize(Widget, XtValueMask *, XSetWindowAttributes *);
static void Destroy(Widget);
static Boolean SetValues(Widget, Widget, Widget, ArgList, Cardinal *);
static void plugin_coords(Widget w, XtPointer p, long *x, long *y)
{
*x = *y = 0;
}
/* actions */
static void AnimatorAction(Widget, XEvent *, String *, Cardinal *);
static XtActionsRec actions[] =
{
{"animator", AnimatorAction},
};
/* translations */
static char translations[] =
"<Key>Return: animator(next) \n\
<Btn1Down>: animator(next) \n\
:<Key>Tab: animator(next) \n\
<Key>N: animator(next) \n\
<Key>P: animator(previous) \n\
<Key>S: animator(stop) \n\
<Key>C: animator(continue) \n\
<Key>Escape: animator(quit) \n\
<Key>Q: animator(quit) \n\
";
AnimatorClassRec animatorClassRec = {
{ /* core fields */
/* superclass */ (WidgetClass) &compositeClassRec,
/* class_name */ "Animator",
/* widget_size */ sizeof(AnimatorRec),
/* class_initialize */ NULL,
/* class_part_initialize */ NULL,
/* class_inited */ FALSE,
/* initialize */ NULL,
/* initialize_hook */ NULL,
/* realize */ Realize,
/* actions */ actions,
/* num_actions */ XtNumber(actions),
/* resources */ resources,
/* num_resources */ XtNumber(resources),
/* xrm_class */ NULLQUARK,
/* compress_motion */ TRUE,
/* compress_exposure */ TRUE,
/* compress_enterleave */ TRUE,
/* visible_interest */ FALSE,
/* destroy */ Destroy,
/* resize */ Resize,
/* expose */ Redisplay,
/* set_values */ SetValues,
/* set_values_hook */ NULL,
/* set_values_almost */ XtInheritSetValuesAlmost,
/* get_values_hook */ NULL,
/* accept_focus */ NULL,
/* version */ XtVersion,
/* callback_private */ NULL,
/* tm_table */ translations,
/* query_geometry */ XtInheritQueryGeometry,
/* display_accelerator */ XtInheritDisplayAccelerator,
/* extension */ NULL
}, {
/* geometry_manager */ GeometryManager,
/* change_managed */ ChangeManaged,
/* insert_child */ XtInheritInsertChild,
/* delete_child */ XtInheritDeleteChild,
/* extension */ NULL
}, { /* animator fields */
/* empty */ 0
}
};
WidgetClass animatorWidgetClass = (WidgetClass)&animatorClassRec;
/* supporting code copied directly from animator.c */
static void ani_stepper(XtPointer client_data, XtIntervalId *id)
{
long ani_then, ani_now;
AnimatorWidget aw = (AnimatorWidget) client_data;
if (!XtIsRealized((Widget)aw)) return;
/* cancel any pending timeout */
/* this is tricky. if ani_stepper is called by us and not by X,
we must remove the timeout ourselves */
if (!id && aw->animator.waiting) {
XtRemoveTimeOut(aw->animator.timeout);
}
aw->animator.waiting = False;
ani_then = ani_now = aw->animator.now;
if (aw->animator.mode == ANI_NEXT || aw->animator.mode == ANI_CONTINUE)
ani_now += aw->animator.delta;
else if (aw->animator.mode == ANI_PREVIOUS)
ani_now -= aw->animator.delta;
if (ani_now < 0)
ani_now = 0;
if (ani_now > aw->animator.duration)
ani_now = 0;
if (ani_now != ani_then) {
aw->animator.now = ani_now;
Redisplay((Widget)aw, NULL, None);
}
if (aw->animator.mode == ANI_CONTINUE) {
aw->animator.timeout = XtAppAddTimeOut(
XtWidgetToApplicationContext((Widget)aw),
aw->animator.delta, ani_stepper,
(XtPointer)aw);
aw->animator.waiting = True;
}
}
static void ani_ctl(Widget w, int mode)
{
AnimatorWidget aw = (AnimatorWidget)w;
switch (mode) {
case ANI_CONTINUE:
if (aw->animator.mode == mode) break;
case ANI_STOP:
case ANI_NEXT:
case ANI_PREVIOUS:
aw->animator.mode = mode;
ani_stepper(w, NULL);
break;
case ANI_QUIT:
/* can't really do that from here, can we? */
default:
break;
}
}
static void AnimatorAction(Widget w, XEvent *event, String *params, Cardinal *n)
{
if (*n < 1 || !cstrcasecmp(params[0], "next"))
ani_ctl(w, ANI_NEXT);
else if (!cstrcasecmp(params[0], "previous"))
ani_ctl(w, ANI_PREVIOUS);
else if (!cstrcasecmp(params[0], "stop"))
ani_ctl(w, ANI_STOP);
else if (!cstrcasecmp(params[0], "continue"))
ani_ctl(w, ANI_CONTINUE);
else if (!cstrcasecmp(params[0], "quit"))
ani_ctl(w, ANI_QUIT);
}
static ani_image *i_list = NULL;
/* try directories in the following order:
0. If absolute path, use it as is
1. Document directory
2. Current directory
3. Home directory
Improvements?
*/
/* This is self-contained: it uses only the static variable i_list to
point to the head of the image list.
*/
ani_image *name2image(Display *display, Window ani_win, char *name)
{
ani_image *i;
int err;
char b[1024];
char *tmpfile = "/tmp/siagimage.xpm";
if (!name) return NULL;
for (i = i_list; i; i = i->next) {
if (!strcmp(name, i->filename)) return i;
}
/* didn't find it; try loading */
sprintf(b, "%s/common/any2xpm %s > %s", siaghome, name, tmpfile);
system(b);
i = (ani_image *)cmalloc(sizeof(ani_image));
i->xa.closeness = 40000;
i->xa.exactColors = FALSE;
i->xa.valuemask = XpmCloseness | XpmExactColors;
err = XpmReadFileToPixmap(display, ani_win,
tmpfile, &(i->pixmap), &(i->mask), &(i->xa));
if (err != XpmSuccess) {
cfree(i);
fprintf(stderr, "XpmReadFileToPixmap returns %s\n",
XpmGetErrorString(err));
return NULL;
}
remove(tmpfile);
i->filename = cstrdup(name);
i->next = i_list;
i_list = i;
return i;
}
/* this looks unnecessarily complex and it is, but I had problems
with signedness and MAXINT wraparound */
static int INTERPOLATE(int x1, int x2,
unsigned long t, unsigned long t1, unsigned long t2)
{
long x0 = x1; /* always pos */
long xd = x2-x1; /* pos or neg */
long td1 = t-t1; /* always pos */
long td2 = t2-t1; /* always pos */
long tmp = xd*td1; /* pos or neg */
long rl = x0+tmp/td2; /* pos or neg */
int ri = rl; /* pos or neg */
return ri;
}
/* create, draw and return a pixmap of the stage at time now.
Caller must free */
/* this is self-contained; no global or static vars are used,
so it can be called at any time from anywhere with any parameters */
Pixmap ani_pixmap(Display *display, Window ani_win, GC ani_gc,
ani_object *cast, char *bg_pixmap, unsigned long ani_now)
{
int font_index, color_index;
Font font;
unsigned long color;
int x, y, x1, y1, x2, y2;
unsigned int b1, d1;
Window root;
long width, height;
unsigned int w1, h1;
int visible;
int angle1, angle2;
ani_object *actor;
ani_image *img, *bg_image;
Pixmap scribble;
Window ani_root;
int ani_x, ani_y;
unsigned int ani_width, ani_height, ani_border, ani_depth;
XGetGeometry(display, ani_win, &ani_root,
&ani_x, &ani_y, &ani_width, &ani_height,
&ani_border, &ani_depth);
if (ani_width > 2000 || ani_height > 2000) return None;
scribble = XCreatePixmap(display, ani_win,
ani_width, ani_height, ani_depth);
/* tile the background */
bg_image = name2image(display, ani_win, bg_pixmap);
if (bg_image) {
int bg_x, bg_y;
unsigned int bg_width, bg_height, bg_border, bg_depth;
XSetClipMask(display, ani_gc, None);
XGetGeometry(display, bg_image->pixmap, &root, &bg_x, &bg_y,
&bg_width, &bg_height, &bg_border, &bg_depth);
for (y = 0; y < ani_height; y += bg_height) {
for (x = 0; x < ani_width; x += bg_width) {
XCopyArea(display, bg_image->pixmap, scribble,
ani_gc, 0, 0, bg_width, bg_height, x, y);
}
}
}
/* now loop over the objects, drawing each in the right place */
for (actor = cast; actor; actor = actor->next) {
/* find the tick before and after ani_now and
interpolate to find position and size.
the script always contains a tick for time=0 */
ani_script *before, *after;
before = actor->script; /* this is never NULL */
while (before->next) {
if (before->next->time > ani_now) break;
before = before->next;
}
after = before->next;
visible = before->visible;
if (!visible) continue;
/* set color */
color_index = actor->color;
color = get_color(display, color_index);
XSetForeground(display, ani_gc, color);
XSetClipMask(display, ani_gc, None);
if (!after) { /* object remains at before */
x = before->x;
y = before->y;
width = before->width;
height = before->height;
} else { /* interpolate */
x = INTERPOLATE(before->x, after->x,
ani_now, before->time, after->time);
y = INTERPOLATE(before->y, after->y,
ani_now, before->time, after->time);
width = INTERPOLATE(before->width, after->width,
ani_now, before->time, after->time);
height = INTERPOLATE(before->height, after->height,
ani_now, before->time, after->time);
}
switch (actor->type) {
case ANI_NONE:
break;
case ANI_LINE:
x2 = x+width;
y2 = y+height;
XDrawLine(display, scribble,
ani_gc, x, y, x2, y2);
break;
case ANI_RECTANGLE:
XDrawRectangle(display, scribble,
ani_gc, x, y, width, height);
break;
case ANI_ARC:
angle1 = 64*90; /* bogus!!! */
angle2 = 64*180;
XDrawArc(display, scribble,
ani_gc, x, y, width, height,
angle1, angle2);
break;
case ANI_ELLIPSE:
XDrawArc(display, scribble,
ani_gc, x, y, width, height,
0, 64*360);
break;
case ANI_PIXMAP:
img = name2image(display, ani_win,
actor->string);
if (!img) break;
XGetGeometry(display, img->pixmap, &root, &x1, &y1,
&w1, &h1, &b1, &d1);
XSetClipOrigin(display, ani_gc, x, y);
XSetClipMask(display, ani_gc, img->mask);
XCopyArea(display, img->pixmap,
scribble, ani_gc,
0, 0, w1, h1, x, y);
break;
case ANI_STRING:
/* set font */
font_index = actor->font;
font = get_font(display, font_index);
XSetFont(display, ani_gc, font);
if (!actor->string) break; /* sanity */
XDrawString(display, scribble,
ani_gc, x, y,
actor->string, strlen(actor->string));
break;
case ANI_POINT:
XDrawPoint(display, scribble,
ani_gc, x, y);
break;
case ANI_FILLRECT:
XFillRectangle(display, scribble,
ani_gc, x, y, width, height);
break;
case ANI_FILLARC:
angle1 = 64*90; /* bogus!!! */
angle2 = 64*180;
XFillArc(display, scribble,
ani_gc, x, y, width, height,
angle1, angle2);
break;
case ANI_FILLELLIPSE:
XFillArc(display, scribble,
ani_gc, x, y, width, height,
0, 64*360);
break;
default:
fprintf(stderr, "bzz\n");
}
}
return scribble;
}
/* end of snip from animator.c */
/* This seems to be the only way to avoid BadDrawable errors */
#define superclass (&coreClassRec)
static void Realize(Widget w, XtValueMask *valueMask,
XSetWindowAttributes *attributes)
{
AnimatorWidget aw = (AnimatorWidget) w;
(*superclass->core_class.realize) (w, valueMask, attributes);
aw->animator.waiting = False;
aw->animator.mode = aw->animator.newmode;
}
static void Destroy(Widget w)
{
AnimatorWidget aw = (AnimatorWidget) w;
aw->animator.mode = ANI_QUIT;
if (aw->animator.waiting)
XtRemoveTimeOut(aw->animator.timeout);
}
static void Redisplay(Widget w, XEvent *xevent, Region r)
{
AnimatorWidget aw = (AnimatorWidget) w;
Pixmap scribble;
Window ani_root;
int ani_x, ani_y;
unsigned int ani_width, ani_height, ani_border, ani_depth;
GC ani_gc;
unsigned long valuemask = 0;
XGCValues values;
/* ah, bullshit. I'll just copy my old ani_redisplay code */
if (!XtIsRealized(w)) return; /* but that doesn't work */
if (!XtIsManaged(w)) return; /* what about this */
if (!aw->animator.cast) {
XClearWindow(XtDisplay(w), XtWindow(w));
return;
}
/* must use Xlib function because we want to change the GC */
ani_gc = XCreateGC(XtDisplay(aw), XtWindow(aw),
valuemask, &values);
XSetGraphicsExposures(XtDisplay(aw), ani_gc, 0);
scribble = ani_pixmap(XtDisplay(aw), XtWindow(aw),
ani_gc, aw->animator.cast,
aw->animator.bg_pixmap, aw->animator.now);
XGetGeometry(XtDisplay(aw), scribble, &ani_root,
&ani_x, &ani_y, &ani_width, &ani_height,
&ani_border, &ani_depth);
XCopyArea(XtDisplay(aw), scribble, XtWindow(aw),
ani_gc, 0, 0,
ani_width, ani_height, 0, 0);
/* and now we don't need this any more */
XFreePixmap(XtDisplay(aw), scribble);
XFreeGC(XtDisplay(aw), ani_gc);
/* update plugin positions */
DoLayout(aw);
}
static Boolean SetValues(Widget current, Widget request, Widget new,
ArgList args, Cardinal *argc)
{
AnimatorWidget curaw = (AnimatorWidget) current;
AnimatorWidget newaw = (AnimatorWidget) new;
Boolean do_redisplay = False;
if (curaw->animator.newmode != newaw->animator.newmode) {
ani_ctl((Widget)newaw, newaw->animator.newmode);
newaw->animator.newmode = ANI_NONE;
}
if ((curaw->animator.now != newaw->animator.now)
/*|| (curaw->animator.mode != newaw->animator.mode)*/) {
#ifdef WANT_FLICKER
do_redisplay = True;
#else
Redisplay(new, NULL, None);
#endif
}
if (curaw->animator.cast != newaw->animator.cast) {
#ifdef WANT_FLICKER
do_redisplay = True;
#else
Redisplay(new, NULL, None);
#endif
}
if (newaw->animator.cast == NULL) do_redisplay = True;
return do_redisplay;
}
/*
* Do a layout, actually assigning positions.
*/
static void DoLayout(AnimatorWidget sw)
{
int i;
long x, y;
for (i = 0; i < sw->composite.num_children; i++) {
x = y = 0;
XtMoveWidget(sw->composite.children[i], x, y);
}
}
/*
* Actually layout the table
*/
static void Resize(Widget w)
{
DoLayout((AnimatorWidget)w);
} /* Resize */
/*
* Geometry Manager
*
* 'reply' is unused; we say only yeay or nay, never almost.
*/
static XtGeometryResult GeometryManager(Widget w,
XtWidgetGeometry request, XtWidgetGeometry reply)
{
return XtGeometryYes;
}
static void ChangeManaged(Widget w)
{
DoLayout((AnimatorWidget)w);
}
|