diff options
Diffstat (limited to 'generic/tkObj.c')
-rw-r--r-- | generic/tkObj.c | 221 |
1 files changed, 125 insertions, 96 deletions
diff --git a/generic/tkObj.c b/generic/tkObj.c index 4c8516e..7affac4 100644 --- a/generic/tkObj.c +++ b/generic/tkObj.c @@ -34,7 +34,7 @@ typedef struct PixelRep { #define SET_COMPLEXPIXEL(objPtr, repPtr) \ (objPtr)->internalRep.twoPtrValue.ptr1 = 0; \ - (objPtr)->internalRep.twoPtrValue.ptr2 = (VOID *) repPtr + (objPtr)->internalRep.twoPtrValue.ptr2 = repPtr #define GET_COMPLEXPIXEL(objPtr) \ ((PixelRep *) (objPtr)->internalRep.twoPtrValue.ptr2) @@ -82,8 +82,8 @@ typedef struct WindowRep { */ static void DupMMInternalRep(Tcl_Obj *srcPtr, Tcl_Obj *copyPtr); -static void DupPixelInternalRep(Tcl_Obj *srcPtr, Tcl_Obj *copyPtr); -static void DupWindowInternalRep(Tcl_Obj *srcPtr,Tcl_Obj *copyPtr); +static void DupPixelInternalRep(Tcl_Obj *srcPtr, Tcl_Obj*copyPtr); +static void DupWindowInternalRep(Tcl_Obj *srcPtr,Tcl_Obj*copyPtr); static void FreeMMInternalRep(Tcl_Obj *objPtr); static void FreePixelInternalRep(Tcl_Obj *objPtr); static void FreeWindowInternalRep(Tcl_Obj *objPtr); @@ -99,7 +99,7 @@ static int SetWindowFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr); * initial display-independant settings. */ -static Tcl_ObjType pixelObjType = { +static const Tcl_ObjType pixelObjType = { "pixel", /* name */ FreePixelInternalRep, /* freeIntRepProc */ DupPixelInternalRep, /* dupIntRepProc */ @@ -113,7 +113,7 @@ static Tcl_ObjType pixelObjType = { * initial display-independant settings. */ -static Tcl_ObjType mmObjType = { +static const Tcl_ObjType mmObjType = { "mm", /* name */ FreeMMInternalRep, /* freeIntRepProc */ DupMMInternalRep, /* dupIntRepProc */ @@ -126,7 +126,7 @@ static Tcl_ObjType mmObjType = { * Tcl object. */ -static Tcl_ObjType windowObjType = { +static const Tcl_ObjType windowObjType = { "window", /* name */ FreeWindowInternalRep, /* freeIntRepProc */ DupWindowInternalRep, /* dupIntRepProc */ @@ -147,9 +147,9 @@ static Tcl_ObjType windowObjType = { */ static ThreadSpecificData * -GetTypeCache() +GetTypeCache(void) { - ThreadSpecificData *tsdPtr = (ThreadSpecificData *) + ThreadSpecificData *tsdPtr = Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); if (tsdPtr->doubleTypePtr == NULL) { @@ -189,10 +189,10 @@ GetPixelsFromObjEx( int *intPtr, double *dblPtr) /* Places to store resulting pixels. */ { - int result,fresh; + int result, fresh; double d; PixelRep *pixelPtr; - static double bias[] = { + static const double bias[] = { 1.0, 10.0, 25.4, 0.35278 /*25.4 / 72.0*/ }; @@ -204,16 +204,16 @@ GetPixelsFromObjEx( */ if (objPtr->typePtr != &pixelObjType) { - ThreadSpecificData *tsdPtr = GetTypeCache(); + ThreadSpecificData *typeCache = GetTypeCache(); - if (objPtr->typePtr == tsdPtr->doubleTypePtr) { + if (objPtr->typePtr == typeCache->doubleTypePtr) { (void) Tcl_GetDoubleFromObj(interp, objPtr, &d); if (dblPtr != NULL) { *dblPtr = d; } *intPtr = (int) d; return TCL_OK; - } else if (objPtr->typePtr == tsdPtr->intTypePtr) { + } else if (objPtr->typePtr == typeCache->intTypePtr) { (void) Tcl_GetIntFromObj(interp, objPtr, intPtr); if (dblPtr) { *dblPtr = (double) (*intPtr); @@ -223,33 +223,31 @@ GetPixelsFromObjEx( } retry: - if (objPtr->typePtr != &pixelObjType) { + fresh = (objPtr->typePtr != &pixelObjType); + if (fresh) { result = SetPixelFromAny(interp, objPtr); if (result != TCL_OK) { return result; } - fresh=1; - } else { - fresh=0; } if (SIMPLE_PIXELREP(objPtr)) { *intPtr = GET_SIMPLEPIXEL(objPtr); if (dblPtr) { - *dblPtr=(double)(*intPtr); + *dblPtr = (double) (*intPtr); } } else { pixelPtr = GET_COMPLEXPIXEL(objPtr); - if ((!fresh) && (pixelPtr->tkwin != tkwin)) - { - /* in case of exo-screen conversions of non-pixels - * we force a recomputation from the string - */ - - FreePixelInternalRep(objPtr); - goto retry; - } - if ((pixelPtr->tkwin != tkwin)||dblPtr) { + if ((!fresh) && (pixelPtr->tkwin != tkwin)) { + /* + * In the case of exo-screen conversions of non-pixels, we force a + * recomputation from the string. + */ + + FreePixelInternalRep(objPtr); + goto retry; + } + if ((pixelPtr->tkwin != tkwin) || dblPtr) { d = pixelPtr->value; if (pixelPtr->units >= 0) { d *= bias[pixelPtr->units] * WidthOfScreen(Tk_Screen(tkwin)); @@ -262,7 +260,7 @@ GetPixelsFromObjEx( } pixelPtr->tkwin = tkwin; if (dblPtr) { - *dblPtr=(double)d; + *dblPtr = d; } } *intPtr = pixelPtr->returnValue; @@ -298,7 +296,7 @@ Tk_GetPixelsFromObj( Tcl_Obj *objPtr, /* The object from which to get pixels. */ int *intPtr) /* Place to store resulting pixels. */ { - return GetPixelsFromObjEx(interp,tkwin,objPtr,intPtr,NULL); + return GetPixelsFromObjEx(interp, tkwin, objPtr, intPtr, NULL); } /* @@ -330,19 +328,22 @@ Tk_GetDoublePixelsFromObj( double *doublePtr) /* Place to store resulting pixels. */ { double d; - int result,val; + int result, val; - result=GetPixelsFromObjEx(interp, tkwin, objPtr, &val, &d); + result = GetPixelsFromObjEx(interp, tkwin, objPtr, &val, &d); if (result != TCL_OK) { return result; } if (objPtr->typePtr == &pixelObjType && !SIMPLE_PIXELREP(objPtr)) { - PixelRep *pixelPtr; - pixelPtr = GET_COMPLEXPIXEL(objPtr); + PixelRep *pixelPtr = GET_COMPLEXPIXEL(objPtr); + if (pixelPtr->units >= 0) { - /* internally "shimmer" to pixel units */ - pixelPtr->units=-1; - pixelPtr->value=d; + /* + * Internally "shimmer" to pixel units. + */ + + pixelPtr->units = -1; + pixelPtr->value = d; } } *doublePtr = d; @@ -375,7 +376,7 @@ FreePixelInternalRep( if (!SIMPLE_PIXELREP(objPtr)) { pixelPtr = GET_COMPLEXPIXEL(objPtr); - ckfree((char *) pixelPtr); + ckfree(pixelPtr); } SET_SIMPLEPIXEL(objPtr, 0); objPtr->typePtr = NULL; @@ -412,7 +413,7 @@ DupPixelInternalRep( SET_SIMPLEPIXEL(copyPtr, GET_SIMPLEPIXEL(srcPtr)); } else { oldPtr = GET_COMPLEXPIXEL(srcPtr); - newPtr = (PixelRep *) ckalloc(sizeof(PixelRep)); + newPtr = ckalloc(sizeof(PixelRep)); newPtr->value = oldPtr->value; newPtr->units = oldPtr->units; newPtr->tkwin = oldPtr->tkwin; @@ -446,7 +447,8 @@ SetPixelFromAny( Tcl_Obj *objPtr) /* The object to convert. */ { const Tcl_ObjType *typePtr; - char *string, *rest; + const char *string; + char *rest; double d; int i, units; @@ -486,7 +488,7 @@ SetPixelFromAny( typePtr = objPtr->typePtr; if ((typePtr != NULL) && (typePtr->freeIntRepProc != NULL)) { - (*typePtr->freeIntRepProc)(objPtr); + typePtr->freeIntRepProc(objPtr); } objPtr->typePtr = &pixelObjType; @@ -495,7 +497,7 @@ SetPixelFromAny( if ((units < 0) && (i == d)) { SET_SIMPLEPIXEL(objPtr, i); } else { - PixelRep *pixelPtr = (PixelRep *) ckalloc(sizeof(PixelRep)); + PixelRep *pixelPtr = ckalloc(sizeof(PixelRep)); pixelPtr->value = d; pixelPtr->units = units; @@ -507,16 +509,8 @@ SetPixelFromAny( error: if (interp != NULL) { - /* - * Must copy string before resetting the result in case a caller is - * trying to convert the interpreter's result to pixels. - */ - - char buf[100]; - - sprintf(buf, "bad screen distance \"%.50s\"", string); - Tcl_ResetResult(interp); - Tcl_AppendResult(interp, buf, NULL); + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "bad screen distance \"%.50s\"", string)); } return TCL_ERROR; } @@ -552,7 +546,7 @@ Tk_GetMMFromObj( int result; double d; MMRep *mmPtr; - static double bias[] = { + static const double bias[] = { 10.0, 25.4, 1.0, 0.35278 /*25.4 / 72.0*/ }; @@ -563,7 +557,7 @@ Tk_GetMMFromObj( } } - mmPtr = (MMRep *) objPtr->internalRep.otherValuePtr; + mmPtr = objPtr->internalRep.otherValuePtr; if (mmPtr->tkwin != tkwin) { d = mmPtr->value; if (mmPtr->units == -1) { @@ -602,7 +596,7 @@ static void FreeMMInternalRep( Tcl_Obj *objPtr) /* MM object with internal rep to free. */ { - ckfree((char *) objPtr->internalRep.otherValuePtr); + ckfree(objPtr->internalRep.otherValuePtr); objPtr->internalRep.otherValuePtr = NULL; objPtr->typePtr = NULL; } @@ -633,13 +627,13 @@ DupMMInternalRep( MMRep *oldPtr, *newPtr; copyPtr->typePtr = srcPtr->typePtr; - oldPtr = (MMRep *) srcPtr->internalRep.otherValuePtr; - newPtr = (MMRep *) ckalloc(sizeof(MMRep)); + oldPtr = srcPtr->internalRep.otherValuePtr; + newPtr = ckalloc(sizeof(MMRep)); newPtr->value = oldPtr->value; newPtr->units = oldPtr->units; newPtr->tkwin = oldPtr->tkwin; newPtr->returnValue = oldPtr->returnValue; - copyPtr->internalRep.otherValuePtr = (VOID *) newPtr; + copyPtr->internalRep.otherValuePtr = newPtr; } /* @@ -669,7 +663,7 @@ UpdateStringOfMM( char buffer[TCL_DOUBLE_SPACE]; register int len; - mmPtr = (MMRep *) objPtr->internalRep.otherValuePtr; + mmPtr = objPtr->internalRep.otherValuePtr; /* assert( mmPtr->units == -1 && objPtr->bytes == NULL ); */ if ((mmPtr->units != -1) || (objPtr->bytes != NULL)) { Tcl_Panic("UpdateStringOfMM: false precondition"); @@ -678,7 +672,7 @@ UpdateStringOfMM( Tcl_PrintDouble(NULL, mmPtr->value, buffer); len = (int)strlen(buffer); - objPtr->bytes = (char *) ckalloc((unsigned) len + 1); + objPtr->bytes = ckalloc(len + 1); strcpy(objPtr->bytes, buffer); objPtr->length = len; } @@ -707,17 +701,18 @@ SetMMFromAny( Tcl_Interp *interp, /* Used for error reporting if not NULL. */ Tcl_Obj *objPtr) /* The object to convert. */ { - ThreadSpecificData *tsdPtr = GetTypeCache(); + ThreadSpecificData *typeCache = GetTypeCache(); const Tcl_ObjType *typePtr; - char *string, *rest; + const char *string; + char *rest; double d; int units; MMRep *mmPtr; - if (objPtr->typePtr == tsdPtr->doubleTypePtr) { + if (objPtr->typePtr == typeCache->doubleTypePtr) { Tcl_GetDoubleFromObj(interp, objPtr, &d); units = -1; - } else if (objPtr->typePtr == tsdPtr->intTypePtr) { + } else if (objPtr->typePtr == typeCache->intTypePtr) { Tcl_GetIntFromObj(interp, objPtr, &units); d = (double) units; units = -1; @@ -779,18 +774,18 @@ SetMMFromAny( typePtr = objPtr->typePtr; if ((typePtr != NULL) && (typePtr->freeIntRepProc != NULL)) { - (*typePtr->freeIntRepProc)(objPtr); + typePtr->freeIntRepProc(objPtr); } - objPtr->typePtr = &mmObjType; + objPtr->typePtr = &mmObjType; - mmPtr = (MMRep *) ckalloc(sizeof(MMRep)); - mmPtr->value = d; - mmPtr->units = units; - mmPtr->tkwin = NULL; + mmPtr = ckalloc(sizeof(MMRep)); + mmPtr->value = d; + mmPtr->units = units; + mmPtr->tkwin = NULL; mmPtr->returnValue = d; - objPtr->internalRep.otherValuePtr = (VOID *) mmPtr; + objPtr->internalRep.otherValuePtr = mmPtr; return TCL_OK; } @@ -823,7 +818,7 @@ TkGetWindowFromObj( Tcl_Obj *objPtr, /* The object from which to get window. */ Tk_Window *windowPtr) /* Place to store resulting window. */ { - TkMainInfo *mainPtr = ((TkWindow *)tkwin)->mainPtr; + TkMainInfo *mainPtr = ((TkWindow *) tkwin)->mainPtr; register WindowRep *winPtr; int result; @@ -832,26 +827,27 @@ TkGetWindowFromObj( return result; } - winPtr = (WindowRep *) objPtr->internalRep.otherValuePtr; - if ( winPtr->tkwin == NULL - || winPtr->mainPtr == NULL - || winPtr->mainPtr != mainPtr - || winPtr->epoch != mainPtr->deletionEpoch) - { - /* Cache is invalid. + winPtr = objPtr->internalRep.otherValuePtr; + if (winPtr->tkwin == NULL + || winPtr->mainPtr == NULL + || winPtr->mainPtr != mainPtr + || winPtr->epoch != mainPtr->deletionEpoch) { + /* + * Cache is invalid. */ + winPtr->tkwin = Tk_NameToWindow(interp, Tcl_GetStringFromObj(objPtr, NULL), tkwin); + if (winPtr->tkwin == NULL) { + /* ASSERT: Tk_NameToWindow has left error message in interp */ + return TCL_ERROR; + } + winPtr->mainPtr = mainPtr; winPtr->epoch = mainPtr ? mainPtr->deletionEpoch : 0; } *windowPtr = winPtr->tkwin; - - if (winPtr->tkwin == NULL) { - /* ASSERT: Tk_NameToWindow has left error message in interp */ - return TCL_ERROR; - } return TCL_OK; } @@ -890,15 +886,15 @@ SetWindowFromAny( Tcl_GetStringFromObj(objPtr, NULL); typePtr = objPtr->typePtr; if ((typePtr != NULL) && (typePtr->freeIntRepProc != NULL)) { - (*typePtr->freeIntRepProc)(objPtr); + typePtr->freeIntRepProc(objPtr); } - winPtr = (WindowRep *) ckalloc(sizeof(WindowRep)); + winPtr = ckalloc(sizeof(WindowRep)); winPtr->tkwin = NULL; winPtr->mainPtr = NULL; winPtr->epoch = 0; - objPtr->internalRep.otherValuePtr = (VOID*)winPtr; + objPtr->internalRep.otherValuePtr = winPtr; objPtr->typePtr = &windowObjType; return TCL_OK; @@ -930,11 +926,11 @@ DupWindowInternalRep( register WindowRep *oldPtr, *newPtr; oldPtr = srcPtr->internalRep.otherValuePtr; - newPtr = (WindowRep *) ckalloc(sizeof(WindowRep)); + newPtr = ckalloc(sizeof(WindowRep)); newPtr->tkwin = oldPtr->tkwin; newPtr->mainPtr = oldPtr->mainPtr; newPtr->epoch = oldPtr->epoch; - copyPtr->internalRep.otherValuePtr = (VOID *)newPtr; + copyPtr->internalRep.otherValuePtr = newPtr; copyPtr->typePtr = srcPtr->typePtr; } @@ -960,13 +956,47 @@ static void FreeWindowInternalRep( Tcl_Obj *objPtr) /* Window object with internal rep to free. */ { - ckfree((char *) objPtr->internalRep.otherValuePtr); + ckfree(objPtr->internalRep.otherValuePtr); objPtr->internalRep.otherValuePtr = NULL; objPtr->typePtr = NULL; } /* - *-------------------------------------------------------------- + *---------------------------------------------------------------------- + * + * TkNewWindowObj -- + * + * This function allocates a new Tcl_Obj that refers to a particular to a + * particular Tk window. + * + * Results: + * A standard Tcl object reference, with refcount 0. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------- + */ + +Tcl_Obj * +TkNewWindowObj( + Tk_Window tkwin) +{ + Tcl_Obj *objPtr = Tcl_NewStringObj(Tk_PathName(tkwin), -1); + TkMainInfo *mainPtr = ((TkWindow *) tkwin)->mainPtr; + register WindowRep *winPtr; + + SetWindowFromAny(NULL, objPtr); + + winPtr = objPtr->internalRep.otherValuePtr; + winPtr->tkwin = tkwin; + winPtr->mainPtr = mainPtr; + winPtr->epoch = mainPtr->deletionEpoch; + return objPtr; +} + +/* + *---------------------------------------------------------------------- * * TkParsePadAmount -- * @@ -984,7 +1014,7 @@ FreeWindowInternalRep( * An error message is written to the interpreter if something is not * right. * - *-------------------------------------------------------------- + *---------------------------------------------------------------------- */ int @@ -1001,12 +1031,12 @@ TkParsePadAmount( Tcl_Obj **objv; /* The objects in the list */ /* - * Check for a common case where a single object would otherwise - * be shimmered between a list and a pixel spec. + * Check for a common case where a single object would otherwise be + * shimmered between a list and a pixel spec. */ if (specObj->typePtr == &pixelObjType) { - if (Tk_GetPixelsFromObj(interp, tkwin, specObj, &firstInt) != TCL_OK) { + if (Tk_GetPixelsFromObj(interp, tkwin, specObj, &firstInt) != TCL_OK){ Tcl_ResetResult(interp); Tcl_AppendResult(interp, "bad pad value \"", Tcl_GetString(specObj), @@ -1097,7 +1127,6 @@ TkRegisterObjTypes(void) Tcl_RegisterObjType(&tkCursorObjType); Tcl_RegisterObjType(&tkFontObjType); Tcl_RegisterObjType(&mmObjType); - Tcl_RegisterObjType(&tkOptionObjType); Tcl_RegisterObjType(&pixelObjType); Tcl_RegisterObjType(&tkStateKeyObjType); Tcl_RegisterObjType(&windowObjType); |