home *** CD-ROM | disk | FTP | other *** search
/ Practical Programming in Tcl & Tk (4th Edition) / TCLBOOK4.BIN / pc / exsource.old / 47_13.c < prev    next >
Text File  |  2003-04-15  |  1KB  |  40 lines

  1. /*
  2.  * Example 47-13
  3.  * ComputeGeometry computes the widget\" s size.
  4.  */
  5.  
  6. static void
  7. ComputeGeometry(Clock *clockPtr)
  8. {
  9.     int width, height;
  10.     Tk_FontMetrics fm;                            /* Font size information */
  11.     struct tm *tmPtr;                            /* Time info split into fields */
  12.     struct timeval tv;                            /* BSD-style time value */
  13.     int bd;                            /* Padding from borders */
  14.     char clock[1000];                            /* Displayed time */
  15.  
  16.     /*
  17.      * Get the time and format it to see how big it will be.
  18.      */
  19.     gettimeofday(&tv, NULL);
  20.     tmPtr = localtime(&tv.tv_sec);
  21.     strftime(clock, 1000, clockPtr->format, tmPtr);
  22.     if (clockPtr->clock != NULL) {
  23.         ckfree(clockPtr->clock);
  24.     }
  25.     clockPtr->clock = ckalloc(1+strlen(clock));
  26.     clockPtr->numChars = strlen(clock);
  27.  
  28.     bd = clockPtr->highlightWidth + clockPtr->borderWidth;
  29.     Tk_GetFontMetrics(clockPtr->tkfont, &fm);
  30.     height = fm.linespace + 2*(bd + clockPtr->padY);
  31.     Tk_MeasureChars(clockPtr->tkfont, clock,
  32.         clockPtr->numChars, 0, 0, &clockPtr->textWidth);
  33.     width = clockPtr->textWidth + 2*(bd + clockPtr->padX);
  34.  
  35.     Tk_GeometryRequest(clockPtr->tkwin, width, height);
  36.     Tk_SetInternalBorder(clockPtr->tkwin, bd);
  37. }
  38.  
  39.  
  40.