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

  1. /*
  2.  * Example 47-8
  3.  * ClockObjConfigure allocates resources for the widget.
  4.  */
  5.  
  6. static int
  7. ClockObjConfigure(interp, clockPtr, objc, objv)
  8.     Tcl_Interp *interp;    /* For return values and errors */
  9.     Clock *clockPtr;                    /* The per-instance data structure */
  10.     int objc;                    /* Number of valid entries in argv */
  11.     Tcl_Obj *objv[];                    /* The command line arguments */
  12. {
  13.     XGCValues gcValues;
  14.     GC newGC;
  15.     Tk_SavedOptions savedOptions;
  16.     int mask, error;
  17.     Tcl_Obj *errorResult;
  18.  
  19.     /*
  20.      * The first time through this loop we set the 
  21.      * configuration from the command line inputs. The second
  22.      * pass is used to restore the configuration in case of
  23.      * errors
  24.      */
  25.     for (error = 0 ; error <= 1 ; error++) {
  26.         if (!error) {
  27.             /*
  28.              * Tk_SetOptions parses the command arguments
  29.              * and looks for defaults in the resource
  30.              * database.
  31.              */
  32.             if (Tk_SetOptions(interp, (char *) clockPtr,
  33.                     clockPtr->optionTable, objc, objv,
  34.                     clockPtr->tkwin, &savedOptions,
  35.                     &mask) != TCL_OK) {
  36.                 continue;
  37.             }
  38.         } else {
  39.             /*
  40.              * Restore options from saved values
  41.              */
  42.             errorResult = Tcl_GetObjResult(interp);
  43.             Tcl_IncrRefCount(errorResult);
  44.             Tk_RestoreSavedOptions(&savedOptions);
  45.         }
  46.         if (mask & GRAPHICS_MASK) {
  47.             /*
  48.              * Give the widget a default background so it doesn\" t
  49.              * get a random background between the time it is 
  50.              * initially displayed by the system and we paint it
  51.              */
  52.             Tk_SetBackgroundFromBorder(clockPtr->tkwin, 
  53.                     clockPtr->background);
  54.             /*
  55.              * Set up the graphics contexts to display the widget.
  56.              * The context is used to draw off-screen pixmaps,
  57.              * so turn off exposure notifications.
  58.              */
  59.             gcValues.background = 
  60.                 Tk_3DBorderColor(clockPtr->background)->pixel;
  61.             gcValues.foreground = clockPtr->foreground->pixel;
  62.             gcValues.font = Tk_FontId(clockPtr->tkfont);
  63.             gcValues.graphics_exposures = False;
  64.             newGC = Tk_GetGC(clockPtr->tkwin,
  65.             GCBackground|GCForeground|GCFont|GCGraphicsExposures,
  66.                 &gcValues);
  67.             if (clockPtr->textGC != None) {
  68.                 Tk_FreeGC(clockPtr->display, clockPtr->textGC);
  69.             }
  70.             clockPtr->textGC = newGC;
  71.         }
  72.         /*
  73.          * Determine how big the widget wants to be.
  74.          */
  75.         if (mask & GEOMETRY_MASK) {
  76.             ComputeGeometry(clockPtr);
  77.         }
  78.         /*
  79.          * Set up a call to display ourself.
  80.          */
  81.         if ((clockPtr->tkwin != NULL) &&
  82.                   Tk_IsMapped(clockPtr->tkwin)
  83.                  && !(clockPtr->flags & REDRAW_PENDING)) {
  84.             Tk_DoWhenIdle(ClockDisplay,
  85.                 (ClientData) clockPtr);
  86.             clockPtr->flags |= REDRAW_PENDING;
  87.         }
  88.         /*
  89.          * All OK, break out and avoid error rollback.
  90.          */
  91.         break;
  92.     }
  93.     if (!error) {
  94.         Tk_FreeSavedOptions(&savedOptions);
  95.         return TCL_OK;
  96.     } else {
  97.         Tcl_SetObjResult(interp, errorResult);
  98.         Tcl_DecrRefCount(errorResult);
  99.         return TCL_ERROR;
  100.     }
  101. }
  102.  
  103.  
  104.