/* From: The X Window System, Programming and Applications with Xt * OSF/Motif Edition, by Douglas Young, Prentice Hall, 1990 * Example described on pages: 149-151 */ #include #include #include #include #include #include #include void update_time(Widget w, XtIntervalId id); void xs_wprintf(); main(int argc, char *argv[]) { Widget toplevel, clock; /* Create the widgets. */ toplevel = XtInitialize(argv[0], "Clock", NULL, 0, &argc, argv); clock = XtCreateManagedWidget("face", xmLabelWidgetClass, toplevel, NULL, 0); /* Get the initial time. */ update_time(clock, 0); XtRealizeWidget(toplevel); XtMainLoop(); } void update_time(Widget w, XtIntervalId id) { time_t tloc, rounded_tloc, next_minute; /* Ask System for the time. */ time(&tloc); /* Convert the time to a string and display it, * after rounding it down to the last minute. */ rounded_tloc = tloc / 60 * 60; xs_wprintf(w, "%s", ctime(&rounded_tloc)); /* Adjust the time to reflect the time till the next round minute. */ next_minute = (60 - tloc % 60) * 1000; /* The Intrinsics removes timeouts when they occur, * so put ourselves back. */ XtAddTimeOut((unsigned long)next_minute, (XtTimerCallbackProc)update_time, w); } /* xs_wprintf: fprintf-like function for XmLabel widgets * From: The X Window System, Programming and Applications with Xt * OSF/Motif Edition by Douglas Young, Prentice Hall, 1990 */ void xs_wprintf(va_alist) va_dcl { Widget w; char *format; va_list args; char str[1000]; /* DANGER: Fixed buffer size */ Arg wargs[1]; XmString xmstr; /* Init the variable length args list. */ va_start(args); /* Extract the destination widget. * Make sure it is a subclass of XmLabel. */ w = va_arg(args, Widget); if(!XtIsSubclass(w, xmLabelWidgetClass)) XtError("xs_wprintf() requires a Label Widget"); /* Extract the format to be used. */ format = va_arg(args, char *); /* Use vsprintf to format the string to be displayed in the * XmLabel widget, then convert it to a compound string */ vsprintf(str, format, args); xmstr = XmStringLtoRCreate(str, XmSTRING_DEFAULT_CHARSET); XtSetArg(wargs[0], XmNlabelString, xmstr); XtSetValues(w, wargs, 1); va_end(args); } /* end xs_wprintf */