static status posixValueDate(Date d, Real r) { time_t v = (time_t)valReal(r); double diff = (double) v - valReal(r); if ( diff < -1.0 || diff > 1.0 ) return errorPce(d, NAME_intRange); d->unix_date = v; succeed; }
void ws_status_timer(Timer tm, Name status) { UINT id; if ( (id = getIdTimer(tm)) ) { KillTimer(NULL, id); deleteHashTable(TimerTable, toInt(id)); setIdTimer(tm, 0); } if ( status != NAME_idle ) { long msec = (long) (valReal(tm->interval) * 1000.0); if ( (id = SetTimer(NULL, 0, (UINT)msec, (TIMERPROC) timer_proc)) ) { if ( !TimerTable ) { TimerTable = globalObject(CtoName("active_timers"), ClassHashTable, EAV); assign(TimerTable, refer, NAME_none); } appendHashTable(TimerTable, toInt(id), tm); setIdTimer(tm, id); DEBUG(NAME_timer, Cprintf("Created timer of %d milliseconds (id = %d)\n", msec, id)); } else Cprintf("Failed SetTimer()\n"); } }
static void includeArrowsInAreaArc(Arc a) { if ( notNil(a->first_arrow) || notNil(a->second_arrow) ) { int sx, sy, ex, ey; int cx, cy; Any av[4]; points_arc(a, &sx, &sy, &ex, &ey); cx = valInt(a->position->x); cy = valInt(a->position->y); if ( notNil(a->first_arrow) ) { av[0] = toInt(sx); av[1] = toInt(sy); if ( valReal(a->size_angle) >= 0.0 ) { av[2] = toInt(sx+(sy-cy)); av[3] = toInt(sy-(sx-cx)); } else { av[2] = toInt(sx-(sy-cy)); av[3] = toInt(sy+(sx-cx)); } if ( qadSendv(a->first_arrow, NAME_points, 4, av) ) { ComputeGraphical(a->first_arrow); unionNormalisedArea(a->area, a->first_arrow->area); } } if ( notNil(a->second_arrow) ) { av[0] = toInt(ex); av[1] = toInt(ey); if ( valReal(a->size_angle) >= 0.0 ) { av[2] = toInt(ex-(ey-cy)); av[3] = toInt(ey+(ex-cx)); } else { av[2] = toInt(ex+(ey-cy)); av[3] = toInt(ey-(ex-cx)); } if ( qadSendv(a->second_arrow, NAME_points, 4, av) ) { ComputeGraphical(a->second_arrow); unionNormalisedArea(a->area, a->second_arrow->area); } } } }
void points_arc(Arc a, int *sx, int *sy, int *ex, int *ey) { int cx = valInt(a->position->x); int cy = valInt(a->position->y); float start = valReal(a->start_angle); float size = valReal(a->size_angle); if ( sx ) *sx = cx + rfloat((float) valInt(a->size->w) * COS(start)); if ( sy ) *sy = cy - rfloat((float) valInt(a->size->h) * SIN(start)); if ( ex ) *ex = cx + rfloat((float) valInt(a->size->w) * COS(start + size)); if ( ey ) *ey = cy - rfloat((float) valInt(a->size->h) * SIN(start + size)); }
static void format_value(Slider s, char *buf, Any val) { int deffmt = isDefault(s->format); if ( isInteger(val) ) sprintf(buf, deffmt ? INTPTR_FORMAT : strName(s->format), valInt(val)); else sprintf(buf, deffmt ? "%g" : strName(s->format), valReal(val)); }
static status angleInArc(Arc a, int angle) { int start = rfloat(valReal(a->start_angle)); int size = rfloat(valReal(a->size_angle)); if ( size < 0 ) { start += size; size = -size; } while(start < 0) start += 360; start = start % 360; if ( (angle >= start && angle <= start + size) || (angle < start && angle <= start + size - 360) ) succeed; fail; }
int ws_read_stream_data(Stream s, void *data, int len, Real timeout) { if ( s->rdfd < 0 ) { errno = EINVAL; return -1; } if ( notDefault(timeout) ) { #ifdef HAVE_POLL double v = valReal(timeout); int to = (int)(v*1000.0); struct pollfd fds[1]; fds[0].fd = s->rdfd; fds[0].events = POLLIN; if ( poll(fds, 1, to) == 0 ) return -2; #else #ifndef __WINDOWS__ if ( s->rdfd < FD_SETSIZE ) #endif { fd_set readfds; struct timeval to; double v = valReal(timeout); to.tv_sec = (long)v; to.tv_usec = (long)(v * 1000000.0) % 1000000; FD_ZERO(&readfds); FD_SET(s->rdfd, &readfds); if ( select(s->rdfd+1, &readfds, NULL, NULL, &to) == 0 ) return -2; } #endif } return read(s->rdfd, data, len); }
status ws_create_font(FontObj f, DisplayObj d) { XpceFontInfo xref; DisplayWsXref r = d->ws_ref; XftFont *xft = NULL; if ( !instanceOfObject(f->x_name, ClassCharArray) || !isstrA(&f->x_name->data) ) /* HACK */ { XftPattern *p = XftPatternCreate(); XftPattern *match; FcResult fcrc; int i; char *fam; int mono = FALSE; Real scale = getClassVariableValueObject(f, NAME_scale); double fscale = (scale ? valReal(scale) : 1.0); if ( f->family == NAME_screen ) { fam = "monospace"; mono = TRUE; } else fam = strName(f->family); XftPatternAddString(p, XFT_FAMILY, fam); XftPatternAddDouble(p, XFT_PIXEL_SIZE, (double)valInt(f->points)*fscale); if ( f->style == NAME_italic ) XftPatternAddInteger(p, XFT_SLANT, XFT_SLANT_ITALIC); else if ( f->style == NAME_roman ) XftPatternAddInteger(p, XFT_SLANT, XFT_SLANT_ROMAN); else if ( f->style == NAME_bold ) XftPatternAddInteger(p, XFT_WEIGHT, XFT_WEIGHT_BOLD); if ( mono ) { DEBUG(NAME_font, Cprintf("Asking for fixed\n")); XftPatternAddInteger(p, XFT_SPACING, XFT_MONO); } if ( !(match = XftFontMatch(r->display_xref, r->screen, p, &fcrc)) ) { DEBUG(NAME_font, Cprintf("XftFontMatch() failed. Calling replaceFont()\n")); return replaceFont(f, d); } #ifdef HAVE_XFTNAMEUNPARSE DEBUG(NAME_font, { char buf[1024]; XftNameUnparse(match, buf, sizeof(buf)); Cprintf("Match = '%s'\n", buf); });
int ws_read_stream_data(Stream s, void *data, int len, Real timeout) { if ( s->rdfd < 0 ) { errno = EINVAL; return -1; } if ( notDefault(timeout) ) { fd_set readfds; struct timeval to; double v = valReal(timeout); to.tv_sec = (long)v; to.tv_usec = (long)(v * 1000000.0) % 1000000; FD_ZERO(&readfds); FD_SET(s->rdfd, &readfds); if ( select(s->rdfd+1, &readfds, NULL, NULL, &to) == 0 ) return -2; } return read(s->rdfd, data, len); }
status ws_dispatch(Int FD, Any timeout) { XtIntervalId tid = 0; XtInputId iid = 0; status rval = SUCCEED; int ofd = dispatch_fd; int fd = (isDefault(FD) ? dispatch_fd : isNil(FD) ? -1 : valInt(FD)); /* No context: wait for input */ /* timeout */ if ( ThePceXtAppContext == NULL ) { int ready; #ifdef HAVE_POLL int to; struct pollfd fds[1]; if ( isNil(timeout) ) { to = -1; } else if ( isDefault(timeout) ) { to = 250; } else if ( isInteger(timeout) ) { to = valInt(timeout); } else if ( instanceOfObject(timeout, ClassReal) ) { to = (int)(valReal(timeout)*1000.0); } else to = 256; fds[0].fd = fd; fds[0].events = POLLIN; ready = poll(fds, 1, to); #else struct timeval to; struct timeval *tp = &to; fd_set readfds; int setmax = 0; if ( isNil(timeout) ) { tp = NULL; } else if ( isDefault(timeout) ) { to.tv_sec = 0; to.tv_usec = 250000; } else { double v; if ( isInteger(timeout) ) v = (double)valInt(timeout)/1000.0; else if ( instanceOfObject(timeout, ClassReal) ) v = valReal(timeout); else v = 0.25; to.tv_sec = (long)v; to.tv_usec = (long)(v * 1000000.0) % 1000000; } FD_ZERO(&readfds); if ( fd >= 0 ) { FD_SET(fd, &readfds); setmax = max(setmax, fd); dispatch_fd = fd; } ready = select(setmax+1, &readfds, NULL, NULL, tp); #endif dispatch_fd = ofd; return (ready > 0 ? SUCCEED : FAIL); } /* A display: dispatch until there */ /* is input or a timeout */ if ( fd >= 0 ) { iid = XtAppAddInput(ThePceXtAppContext, fd, (XtPointer) XtInputReadMask, is_pending, NULL); dispatch_fd = fd; } if ( notNil(timeout) ) { long to = -1; if ( isInteger(timeout) ) to = valInt(timeout); else if ( instanceOfObject(timeout, ClassReal) ) to = (long)(valReal(timeout)*1000.0); if ( to > 0 ) tid = XtAppAddTimeOut(ThePceXtAppContext, to, is_timeout, (XtPointer) &rval); } DEBUG(NAME_dispatch, Cprintf("Dispatch: timeout = %s, tid = %p\n", pp(timeout), (void*)tid)); if ( pceMTTryLock(LOCK_PCE) ) { RedrawDisplayManager(TheDisplayManager()); pceMTUnlock(LOCK_PCE); } /* All callbacks must be locked! */ XtAppProcessEvent(ThePceXtAppContext, XtIMXEvent|XtIMTimer|XtIMAlternateInput); if ( tid && rval ) /* if rval = FAIL, we had a timeout */ XtRemoveTimeOut(tid); if ( iid ) XtRemoveInput(iid); dispatch_fd = ofd; considerLocStillEvent(); return rval; }
status ws_create_font(FontObj f, DisplayObj d) { WsFont wsf = alloc(sizeof(ws_font)); #ifdef __WINDOWS__ int widths[FONTTABLESIZE]; #else short widths[FONTTABLESIZE]; #endif HDC hdc; HFONT old; int n; TEXTMETRIC tm; int stock; if ( sscanf(strName(f->x_name), STOCKFMT, &stock) == 1 ) { wsf->hfont = GetStockObject(stock); wsf->from_stock = TRUE; } else { LOGFONT lfont; Real scale = getClassVariableValueObject(f, NAME_scale); float fscale = (scale ? valReal(scale) : 1.4); memset(&lfont, 0, sizeof(lfont)); lfont.lfHeight = (int)((float) valInt(f->points) * fscale); lfont.lfWeight = (f->style == NAME_bold ? FW_BOLD : FW_NORMAL); lfont.lfItalic = ((f->style == NAME_italic || f->style == NAME_oblique) ? 1 : 0); lfont.lfPitchAndFamily = (f->family == NAME_screen ? FIXED_PITCH : DEFAULT_PITCH); lfont.lfPitchAndFamily |= (f->family == NAME_helvetica ? FF_SWISS : f->family == NAME_times ? FF_ROMAN : f->family == NAME_screen ? FF_MODERN : FF_DONTCARE); if ( f->family == NAME_symbol ) strcpy(lfont.lfFaceName, "symbol"); if ( instanceOfObject(f->x_name, ClassCharArray) ) { strcpy(lfont.lfFaceName, strName(f->family)); parse_font(strName(f->x_name), &lfont); } else { lfont.lfOutPrecision = OUT_TT_ONLY_PRECIS; lfont.lfQuality = PROOF_QUALITY; } if ( !(wsf->hfont = CreateFontIndirect(&lfont)) ) { Cprintf("Failed to create logical font; replacing with stock font\n"); if ( f->family == NAME_screen ) { if ( f->style == NAME_bold ) stock = SYSTEM_FIXED_FONT; else stock = ANSI_FIXED_FONT; } else { if ( f->style == NAME_bold ) stock = SYSTEM_FONT; else stock = ANSI_VAR_FONT; } wsf->hfont = GetStockObject(stock); wsf->from_stock = TRUE; } else wsf->from_stock = FALSE; } wsf->widths = alloc(FONTTABLESIZE * sizeof(cwidth)); assign(f, iswide, OFF); hdc = GetDC(NULL); old = SelectObject(hdc, wsf->hfont); GetCharWidth(hdc, 0, FONTTABLESIZE-1, widths); for(n=0; n<FONTTABLESIZE; n++) wsf->widths[n] = widths[n]; GetTextMetrics(hdc, &tm); wsf->ascent = tm.tmAscent + tm.tmExternalLeading; wsf->descent = tm.tmDescent; /*if ( !(tm.tmPitchAndFamily & TMPF_TRUETYPE) && f->family != NAME_win ) Cprintf("%s (%s/%s): not a TrueType font\n", pp(f), pp(f->family), pp(f->style)); */ if ( isDefault(f->x_name) ) { char buf[256]; if ( GetTextFace(hdc, sizeof(buf), buf) ) assign(f, x_name, CtoName(buf)); } SelectObject(hdc, old); ReleaseDC(NULL, hdc); if ( wsf->widths['i'] == wsf->widths['w'] ) assign(f, fixed_width, ON); else assign(f, fixed_width, OFF); registerXrefObject(f, d, wsf); succeed; }
static double convert_value(Any val) { return isInteger(val) ? (double)valInt(val) : valReal(val); }
static status RedrawAreaArc(Arc a, Area area) { int x, y, w, h; int aw = valInt(a->size->w); int ah = valInt(a->size->h); int sx, sy, ex, ey; int cx, cy; initialiseDeviceGraphical(a, &x, &y, &w, &h); points_arc(a, &sx, &sy, &ex, &ey); cx = valInt(a->position->x); cy = valInt(a->position->y); r_thickness(valInt(a->pen)); r_dash(a->texture); #ifndef WIN32_GRAPHICS r_arcmode(a->close == NAME_none ? NAME_pieSlice : a->close); r_arc(valInt(a->position->x) - aw, valInt(a->position->y) - ah, 2*aw, 2*ah, rfloat(valReal(a->start_angle)*64.0), rfloat(valReal(a->size_angle)*64.0), a->fill_pattern); if ( a->close != NAME_none && a->pen != ZERO ) { if ( a->close == NAME_chord ) { r_line(sx, sy, ex, ey); } else /* if ( a->close == NAME_pieSlice ) */ { r_line(cx, cy, sx, sy); r_line(cx, cy, ex, ey); } } #else /*WIN32_GRAPHICS*/ { int ax, ay, bx, by; double sa = valReal(a->size_angle); int large; if ( sa >= 0.0 ) { ax = sx, ay = sy, bx = ex, by = ey; large = (sa >= 180.0); } else { ax = ex, ay = ey, bx = sx, by = sy; large = (sa <= -180.0); } r_msarc(valInt(a->position->x) - aw, valInt(a->position->y) - ah, 2*aw, 2*ah, ax, ay, bx, by, large, a->close, a->fill_pattern); } #endif /* __WINDOWS__ */ if (notNil(a->first_arrow)) { Any av[4]; av[0] = toInt(sx); av[1] = toInt(sy); if ( valReal(a->size_angle) >= 0.0 ) { av[2] = toInt(sx+(sy-cy)); av[3] = toInt(sy-(sx-cx)); } else { av[2] = toInt(sx-(sy-cy)); av[3] = toInt(sy+(sx-cx)); } if ( qadSendv(a->first_arrow, NAME_points, 4, av) ) { assign(a->first_arrow, displayed, ON); ComputeGraphical(a->first_arrow); RedrawArea(a->first_arrow, area); } } if (notNil(a->second_arrow)) { Any av[4]; av[0] = toInt(ex); av[1] = toInt(ey); if ( valReal(a->size_angle) >= 0.0 ) { av[2] = toInt(ex-(ey-cy)); av[3] = toInt(ey+(ex-cx)); } else { av[2] = toInt(ex+(ey-cy)); av[3] = toInt(ey-(ex-cx)); } if ( qadSendv(a->second_arrow, NAME_points, 4, av) ) { assign(a->second_arrow, displayed, ON); ComputeGraphical(a->second_arrow); RedrawArea(a->second_arrow, area); } } return RedrawAreaGraphical(a, area); }