diff options
author | donal.k.fellows@manchester.ac.uk <dkf> | 2008-11-26 15:56:37 (GMT) |
---|---|---|
committer | donal.k.fellows@manchester.ac.uk <dkf> | 2008-11-26 15:56:37 (GMT) |
commit | 4cc4bd8988eb43ed337bcbe63481322726dd97d9 (patch) | |
tree | 32207129bc4af526d0f74885fdececc8a860f965 | |
parent | 16cb03c01a0e48ae4f1924ad2e4d7be990d0f93c (diff) | |
download | tk-4cc4bd8988eb43ed337bcbe63481322726dd97d9.zip tk-4cc4bd8988eb43ed337bcbe63481322726dd97d9.tar.gz tk-4cc4bd8988eb43ed337bcbe63481322726dd97d9.tar.bz2 |
Making code neater...
-rw-r--r-- | generic/tk3d.c | 45 | ||||
-rw-r--r-- | unix/tkUnixKey.c | 146 | ||||
-rw-r--r-- | unix/tkUnixMenu.c | 35 |
3 files changed, 104 insertions, 122 deletions
diff --git a/generic/tk3d.c b/generic/tk3d.c index 9570ee4..e247619 100644 --- a/generic/tk3d.c +++ b/generic/tk3d.c @@ -10,7 +10,7 @@ * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tk3d.c,v 1.22 2008/11/08 18:44:39 dkf Exp $ + * RCS: @(#) $Id: tk3d.c,v 1.23 2008/11/26 15:57:24 dkf Exp $ */ #include "tkInt.h" @@ -91,7 +91,7 @@ Tk_Alloc3DBorderFromObj( if (objPtr->typePtr != &tkBorderObjType) { InitBorderObj(objPtr); } - borderPtr = (TkBorder *) objPtr->internalRep.twoPtrValue.ptr1; + borderPtr = objPtr->internalRep.twoPtrValue.ptr1; /* * If the object currently points to a TkBorder, see if it's the one we @@ -130,16 +130,16 @@ Tk_Alloc3DBorderFromObj( */ if (borderPtr != NULL) { - TkBorder *firstBorderPtr = - (TkBorder *) Tcl_GetHashValue(borderPtr->hashPtr); + TkBorder *firstBorderPtr = Tcl_GetHashValue(borderPtr->hashPtr); + FreeBorderObjProc(objPtr); for (borderPtr = firstBorderPtr ; borderPtr != NULL; borderPtr = borderPtr->nextPtr) { if ((Tk_Screen(tkwin) == borderPtr->screen) - && (Tk_Colormap(tkwin) == borderPtr->colormap)) { + && (Tk_Colormap(tkwin) == borderPtr->colormap)) { borderPtr->resourceRefCount++; borderPtr->objRefCount++; - objPtr->internalRep.twoPtrValue.ptr1 = (void *) borderPtr; + objPtr->internalRep.twoPtrValue.ptr1 = borderPtr; return (Tk_3DBorder) borderPtr; } } @@ -151,7 +151,7 @@ Tk_Alloc3DBorderFromObj( borderPtr = (TkBorder *) Tk_Get3DBorder(interp, tkwin, Tcl_GetString(objPtr)); - objPtr->internalRep.twoPtrValue.ptr1 = (void *) borderPtr; + objPtr->internalRep.twoPtrValue.ptr1 = borderPtr; if (borderPtr != NULL) { borderPtr->objRefCount++; } @@ -203,7 +203,7 @@ Tk_Get3DBorder( hashPtr = Tcl_CreateHashEntry(&dispPtr->borderTable, colorName, &isNew); if (!isNew) { - existingBorderPtr = (TkBorder *) Tcl_GetHashValue(hashPtr); + existingBorderPtr = Tcl_GetHashValue(hashPtr); for (borderPtr = existingBorderPtr; borderPtr != NULL; borderPtr = borderPtr->nextPtr) { if ((Tk_Screen(tkwin) == borderPtr->screen) @@ -428,7 +428,7 @@ Tk_Free3DBorder( return; } - prevPtr = (TkBorder *) Tcl_GetHashValue(borderPtr->hashPtr); + prevPtr = Tcl_GetHashValue(borderPtr->hashPtr); TkpFreeBorder(borderPtr); if (borderPtr->bgColorPtr != NULL) { Tk_FreeColor(borderPtr->bgColorPtr); @@ -522,7 +522,7 @@ static void FreeBorderObjProc( Tcl_Obj *objPtr) /* The object we are releasing. */ { - TkBorder *borderPtr = (TkBorder *) objPtr->internalRep.twoPtrValue.ptr1; + TkBorder *borderPtr = objPtr->internalRep.twoPtrValue.ptr1; if (borderPtr != NULL) { borderPtr->objRefCount--; @@ -557,10 +557,10 @@ DupBorderObjProc( Tcl_Obj *srcObjPtr, /* The object we are copying from. */ Tcl_Obj *dupObjPtr) /* The object we are copying to. */ { - TkBorder *borderPtr = (TkBorder *) srcObjPtr->internalRep.twoPtrValue.ptr1; + TkBorder *borderPtr = srcObjPtr->internalRep.twoPtrValue.ptr1; dupObjPtr->typePtr = srcObjPtr->typePtr; - dupObjPtr->internalRep.twoPtrValue.ptr1 = (void *) borderPtr; + dupObjPtr->internalRep.twoPtrValue.ptr1 = borderPtr; if (borderPtr != NULL) { borderPtr->objRefCount++; @@ -982,8 +982,8 @@ Tk_Fill3DRectangle( if ((width > doubleBorder) && (height > doubleBorder)) { XFillRectangle(Tk_Display(tkwin), drawable, borderPtr->bgGC, x + borderWidth, y + borderWidth, - (unsigned int) (width - doubleBorder), - (unsigned int) (height - doubleBorder)); + (unsigned) (width - doubleBorder), + (unsigned) (height - doubleBorder)); } if (borderWidth) { Tk_Draw3DRectangle(tkwin, drawable, border, x, y, width, @@ -1251,7 +1251,7 @@ Tk_Get3DBorderFromObj( * cached in the internal representation of the Tcl_Obj. Check it out... */ - borderPtr = (TkBorder *) objPtr->internalRep.twoPtrValue.ptr1; + borderPtr = objPtr->internalRep.twoPtrValue.ptr1; if ((borderPtr != NULL) && (borderPtr->resourceRefCount > 0) && (Tk_Screen(tkwin) == borderPtr->screen) @@ -1279,12 +1279,12 @@ Tk_Get3DBorderFromObj( if (hashPtr == NULL) { goto error; } - for (borderPtr = (TkBorder *) Tcl_GetHashValue(hashPtr); - (borderPtr != NULL); borderPtr = borderPtr->nextPtr) { + for (borderPtr = Tcl_GetHashValue(hashPtr); borderPtr != NULL; + borderPtr = borderPtr->nextPtr) { if ((Tk_Screen(tkwin) == borderPtr->screen) && (Tk_Colormap(tkwin) == borderPtr->colormap)) { FreeBorderObjProc(objPtr); - objPtr->internalRep.twoPtrValue.ptr1 = (void *) borderPtr; + objPtr->internalRep.twoPtrValue.ptr1 = borderPtr; borderPtr->objRefCount++; return (Tk_3DBorder) borderPtr; } @@ -1362,20 +1362,21 @@ TkDebugBorder( * (not currently used). */ char *name) /* Name of the desired color. */ { - TkBorder *borderPtr; Tcl_HashEntry *hashPtr; - Tcl_Obj *resultPtr, *objPtr; + Tcl_Obj *resultPtr; TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr; resultPtr = Tcl_NewObj(); hashPtr = Tcl_FindHashEntry(&dispPtr->borderTable, name); if (hashPtr != NULL) { - borderPtr = (TkBorder *) Tcl_GetHashValue(hashPtr); + TkBorder *borderPtr = Tcl_GetHashValue(hashPtr); + if (borderPtr == NULL) { Tcl_Panic("TkDebugBorder found empty hash table entry"); } for ( ; (borderPtr != NULL); borderPtr = borderPtr->nextPtr) { - objPtr = Tcl_NewObj(); + Tcl_Obj *objPtr = Tcl_NewObj(); + Tcl_ListObjAppendElement(NULL, objPtr, Tcl_NewIntObj(borderPtr->resourceRefCount)); Tcl_ListObjAppendElement(NULL, objPtr, diff --git a/unix/tkUnixKey.c b/unix/tkUnixKey.c index a800d73..77e782f 100644 --- a/unix/tkUnixKey.c +++ b/unix/tkUnixKey.c @@ -9,7 +9,7 @@ * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tkUnixKey.c,v 1.14 2008/06/11 00:41:43 jenglish Exp $ + * RCS: @(#) $Id: tkUnixKey.c,v 1.15 2008/11/26 15:56:37 dkf Exp $ */ #include "tkInt.h" @@ -24,7 +24,7 @@ * Tk_SetCaretPos -- * * This enables correct placement of the XIM caret. This is called by - * widgets to indicate their cursor placement. This is currently only + * widgets to indicate their cursor placement. This is currently only * used for over-the-spot XIM. * *---------------------------------------------------------------------- @@ -40,11 +40,10 @@ Tk_SetCaretPos( TkWindow *winPtr = (TkWindow *) tkwin; TkDisplay *dispPtr = winPtr->dispPtr; - if ( dispPtr->caret.winPtr == winPtr - && dispPtr->caret.x == x - && dispPtr->caret.y == y - && dispPtr->caret.height == height) - { + if ((dispPtr->caret.winPtr == winPtr) + && (dispPtr->caret.x == x) + && (dispPtr->caret.y == y) + && (dispPtr->caret.height == height)) { return; } @@ -53,22 +52,21 @@ Tk_SetCaretPos( dispPtr->caret.y = y; dispPtr->caret.height = height; -#ifdef TK_USE_INPUT_METHODS /* * Adjust the XIM caret position. */ - if ( (dispPtr->flags & TK_DISPLAY_USE_IM) - && (dispPtr->inputStyle & XIMPreeditPosition) - && (winPtr->inputContext != NULL) ) - { + +#ifdef TK_USE_INPUT_METHODS + if ((dispPtr->flags & TK_DISPLAY_USE_IM) + && (dispPtr->inputStyle & XIMPreeditPosition) + && (winPtr->inputContext != NULL)) { XVaNestedList preedit_attr; XPoint spot; spot.x = dispPtr->caret.x; spot.y = dispPtr->caret.y + dispPtr->caret.height; preedit_attr = XVaCreateNestedList(0, XNSpotLocation, &spot, NULL); - XSetICValues(winPtr->inputContext, - XNPreeditAttributes, preedit_attr, + XSetICValues(winPtr->inputContext, XNPreeditAttributes, preedit_attr, NULL); XFree(preedit_attr); } @@ -82,13 +80,13 @@ Tk_SetCaretPos( * * Convert a keyboard event to a UTF-8 string using XLookupString. * - * This is used as a fallback instead of Xutf8LookupString - * or XmbLookupString if input methods are turned off - * and for KeyRelease events. + * This is used as a fallback instead of Xutf8LookupString or + * XmbLookupString if input methods are turned off and for KeyRelease + * events. * * Notes: - * XLookupString() normally returns a single ISO Latin 1 - * or ASCII control character. + * XLookupString() normally returns a single ISO Latin 1 or ASCII control + * character. * *---------------------------------------------------------------------- */ @@ -104,13 +102,15 @@ TkpGetChar( buf[len] = '\0'; if (len == 1) { - len = Tcl_UniCharToUtf((unsigned char)buf[0], Tcl_DStringValue(dsPtr)); + len = Tcl_UniCharToUtf((unsigned char) buf[0], + Tcl_DStringValue(dsPtr)); Tcl_DStringSetLength(dsPtr, len); } else { /* * len > 1 should only happen if someone has called XRebindKeysym(). * Assume UTF-8. */ + Tcl_DStringSetLength(dsPtr, len); strncpy(Tcl_DStringValue(dsPtr), buf, len); } @@ -140,22 +140,25 @@ TkpGetString( TkWindow *winPtr, /* Window where event occurred */ XEvent *eventPtr, /* X keyboard event. */ Tcl_DString *dsPtr) /* Initialized, empty string to hold result. */ -#ifdef TK_USE_INPUT_METHODS -#if X_HAVE_UTF8_STRING { +#ifdef TK_USE_INPUT_METHODS if ((winPtr->dispPtr->flags & TK_DISPLAY_USE_IM) && (winPtr->inputContext != NULL) - && (eventPtr->type == KeyPress)) - { + && (eventPtr->type == KeyPress)) { int len; Status status; +#if X_HAVE_UTF8_STRING Tcl_DStringSetLength(dsPtr, TCL_DSTRING_STATIC_SIZE-1); len = Xutf8LookupString(winPtr->inputContext, &eventPtr->xkey, Tcl_DStringValue(dsPtr), Tcl_DStringLength(dsPtr), NULL, &status); - if (status == XBufferOverflow) { /* Expand buffer and try again */ + if (status == XBufferOverflow) { + /* + * Expand buffer and try again. + */ + Tcl_DStringSetLength(dsPtr, len); len = Xutf8LookupString(winPtr->inputContext, &eventPtr->xkey, Tcl_DStringValue(dsPtr), Tcl_DStringLength(dsPtr), @@ -166,29 +169,15 @@ TkpGetString( len = 0; } Tcl_DStringSetLength(dsPtr, len); - - return Tcl_DStringValue(dsPtr); - } else { - return TkpGetChar(eventPtr, dsPtr); - } -} #else /* !X_HAVE_UTF8_STRING */ -{ - int len; - Tcl_DString buf; - Status status; - - /* - * Overallocate the dstring to the maximum stack amount. - */ - - Tcl_DStringInit(&buf); - Tcl_DStringSetLength(&buf, TCL_DSTRING_STATIC_SIZE-1); + Tcl_DString buf; /* Holds string in system encoding. */ - if ((winPtr->dispPtr->flags & TK_DISPLAY_USE_IM) - && (winPtr->inputContext != NULL) - && (eventPtr->type == KeyPress)) { + /* + * Overallocate the dstring to the maximum stack amount. + */ + Tcl_DStringInit(&buf); + Tcl_DStringSetLength(&buf, TCL_DSTRING_STATIC_SIZE-1); len = XmbLookupString(winPtr->inputContext, &eventPtr->xkey, Tcl_DStringValue(&buf), Tcl_DStringLength(&buf), NULL, &status); @@ -205,24 +194,16 @@ TkpGetString( if ((status != XLookupChars) && (status != XLookupBoth)) { len = 0; } - } else { - return TkpGetChar(eventPtr, dsPtr); - } - - Tcl_DStringSetLength(&buf, len); - Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(&buf), len, dsPtr); - Tcl_DStringFree(&buf); - - return Tcl_DStringValue(dsPtr); -} - + Tcl_DStringSetLength(&buf, len); + Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(&buf), len, dsPtr); + Tcl_DStringFree(&buf); #endif /* X_HAVE_UTF8_STRING */ -#else /* !TK_USE_INPUT_METHODS */ -{ + + return Tcl_DStringValue(dsPtr); + } +#endif /* TK_USE_INPUT_METHODS */ return TkpGetChar(eventPtr, dsPtr); } -#endif - /* * When mapping from a keysym to a keycode, need information about the @@ -236,30 +217,27 @@ TkpSetKeycodeAndState( KeySym keySym, XEvent *eventPtr) { - Display *display; + Display *display = Tk_Display(tkwin); int state; KeyCode keycode; - display = Tk_Display(tkwin); - if (keySym == NoSymbol) { keycode = 0; } else { keycode = XKeysymToKeycode(display, keySym); - } - if (keycode != 0) { - for (state = 0; state < 4; state++) { - if (XKeycodeToKeysym(display, keycode, state) == keySym) { - if (state & 1) { - eventPtr->xkey.state |= ShiftMask; + if (keycode != 0) { + for (state = 0; state < 4; state++) { + if (XKeycodeToKeysym(display, keycode, state) == keySym) { + if (state & 1) { + eventPtr->xkey.state |= ShiftMask; + } + if (state & 2) { + TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr; + + eventPtr->xkey.state |= dispPtr->modeModMask; + } + break; } - if (state & 2) { - TkDisplay *dispPtr; - - dispPtr = ((TkWindow *) tkwin)->dispPtr; - eventPtr->xkey.state |= dispPtr->modeModMask; - } - break; } } } @@ -412,7 +390,7 @@ TkpInitKeymapInfo( dispPtr->metaModMask = 0; dispPtr->altModMask = 0; codePtr = modMapPtr->modifiermap; - max = 8*modMapPtr->max_keypermod; + max = 8 * modMapPtr->max_keypermod; for (i = 0; i < max; i++, codePtr++) { if (*codePtr == 0) { continue; @@ -451,23 +429,27 @@ TkpInitKeymapInfo( for (j = 0; j < dispPtr->numModKeyCodes; j++) { if (dispPtr->modKeyCodes[j] == *codePtr) { + /* + * 'continue' the outer loop. + */ + goto nextModCode; } } if (dispPtr->numModKeyCodes >= arraySize) { - KeyCode *new; + KeyCode *newCodes; /* * Ran out of space in the array; grow it. */ arraySize *= 2; - new = (KeyCode *) + newCodes = (KeyCode *) ckalloc((unsigned) (arraySize * sizeof(KeyCode))); - memcpy(new, dispPtr->modKeyCodes, - (dispPtr->numModKeyCodes * sizeof(KeyCode))); + memcpy(newCodes, dispPtr->modKeyCodes, + dispPtr->numModKeyCodes * sizeof(KeyCode)); ckfree((char *) dispPtr->modKeyCodes); - dispPtr->modKeyCodes = new; + dispPtr->modKeyCodes = newCodes; } dispPtr->modKeyCodes[dispPtr->numModKeyCodes] = *codePtr; dispPtr->numModKeyCodes++; diff --git a/unix/tkUnixMenu.c b/unix/tkUnixMenu.c index ec77732..6d3b7be 100644 --- a/unix/tkUnixMenu.c +++ b/unix/tkUnixMenu.c @@ -8,7 +8,7 @@ * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tkUnixMenu.c,v 1.20 2008/04/27 22:39:13 dkf Exp $ + * RCS: @(#) $Id: tkUnixMenu.c,v 1.21 2008/11/26 15:56:37 dkf Exp $ */ #include "default.h" @@ -330,6 +330,8 @@ GetMenuIndicatorGeometry( int *widthPtr, /* The resulting width */ int *heightPtr) /* The resulting height */ { + int borderWidth; + if ((mePtr->type == CHECK_BUTTON_ENTRY) || (mePtr->type == RADIO_BUTTON_ENTRY)) { if (!mePtr->hideMargin && mePtr->indicatorOn) { @@ -354,23 +356,18 @@ GetMenuIndicatorGeometry( } } } else { - int borderWidth; - - Tk_GetPixelsFromObj(NULL, menuPtr->tkwin, - menuPtr->borderWidthPtr, &borderWidth); + Tk_GetPixelsFromObj(NULL, menuPtr->tkwin, menuPtr->borderWidthPtr, + &borderWidth); *heightPtr = 0; *widthPtr = borderWidth; } } else { - int borderWidth; - Tk_GetPixelsFromObj(NULL, menuPtr->tkwin, menuPtr->borderWidthPtr, &borderWidth); *heightPtr = 0; *widthPtr = borderWidth; } } - /* *---------------------------------------------------------------------- @@ -400,9 +397,8 @@ GetMenuAccelGeometry( *heightPtr = fmPtr->linespace; if (mePtr->type == CASCADE_ENTRY) { *widthPtr = 2 * CASCADE_ARROW_WIDTH; - } else if ((menuPtr->menuType != MENUBAR) - && (mePtr->accelPtr != NULL)) { - char *accel = Tcl_GetStringFromObj(mePtr->accelPtr, NULL); + } else if ((menuPtr->menuType != MENUBAR) && (mePtr->accelPtr != NULL)) { + char *accel = Tcl_GetString(mePtr->accelPtr); *widthPtr = Tk_TextWidth(tkfont, accel, mePtr->accelLength); } else { @@ -521,7 +517,7 @@ DrawMenuEntryAccelerator( (menuPtr->postedCascade == mePtr) ? TK_RELIEF_SUNKEN : TK_RELIEF_RAISED); } else if (mePtr->accelPtr != NULL) { - char *accel = Tcl_GetStringFromObj(mePtr->accelPtr, NULL); + char *accel = Tcl_GetString(mePtr->accelPtr); int left = x + mePtr->labelWidth + activeBorderWidth + mePtr->indicatorSpace; @@ -703,12 +699,14 @@ DrawMenuEntryLabel( haveImage = 1; } else if (mePtr->bitmapPtr != NULL) { Pixmap bitmap = Tk_GetBitmapFromObj(menuPtr->tkwin, mePtr->bitmapPtr); + Tk_SizeOfBitmap(menuPtr->display, bitmap, &imageWidth, &imageHeight); haveImage = 1; } if (!haveImage || (mePtr->compound != COMPOUND_NONE)) { if (mePtr->labelLength > 0) { - char *label = Tcl_GetStringFromObj(mePtr->labelPtr, NULL); + char *label = Tcl_GetString(mePtr->labelPtr); + textWidth = Tk_TextWidth(tkfont, label, mePtr->labelLength); textHeight = fmPtr->linespace; haveText = 1; @@ -721,6 +719,7 @@ DrawMenuEntryLabel( if (haveImage && haveText) { int fullWidth = (imageWidth > textWidth ? imageWidth : textWidth); + switch ((enum compound) mePtr->compound) { case COMPOUND_TOP: textXOffset = (fullWidth - textWidth)/2; @@ -803,7 +802,7 @@ DrawMenuEntryLabel( int baseline = y + (height + fmPtr->ascent - fmPtr->descent) / 2; if (mePtr->labelLength > 0) { - char *label = Tcl_GetStringFromObj(mePtr->labelPtr, NULL); + char *label = Tcl_GetString(mePtr->labelPtr); Tk_DrawChars(menuPtr->display, d, gc, tkfont, label, mePtr->labelLength, leftEdge + textXOffset, @@ -867,7 +866,7 @@ DrawMenuUnderline( int activeBorderWidth, leftEdge; const char *label, *start, *end; - label = Tcl_GetStringFromObj(mePtr->labelPtr, NULL); + label = Tcl_GetString(mePtr->labelPtr); start = Tcl_UtfAtIndex(label, mePtr->underline); end = Tcl_UtfNext(start); @@ -1076,7 +1075,6 @@ TkpComputeMenubarGeometry( if (mePtr->entryFlags & ENTRY_HELP_MENU) { helpMenuIndex = i; } else if (x + mePtr->width + borderWidth > maxWindowWidth) { - if (i == lastRowBreak) { mePtr->y = y; mePtr->x = x; @@ -1473,6 +1471,7 @@ GetMenuLabelGeometry( haveImage = 1; } else if (mePtr->bitmapPtr != NULL) { Pixmap bitmap = Tk_GetBitmapFromObj(menuPtr->tkwin, mePtr->bitmapPtr); + Tk_SizeOfBitmap(menuPtr->display, bitmap, widthPtr, heightPtr); haveImage = 1; } else { @@ -1491,9 +1490,9 @@ GetMenuLabelGeometry( if (mePtr->labelPtr != NULL) { int textWidth; - char *label = Tcl_GetStringFromObj(mePtr->labelPtr, NULL); - textWidth = Tk_TextWidth(tkfont, label, mePtr->labelLength); + char *label = Tcl_GetString(mePtr->labelPtr); + textWidth = Tk_TextWidth(tkfont, label, mePtr->labelLength); if ((mePtr->compound != COMPOUND_NONE) && haveImage) { switch ((enum compound) mePtr->compound) { case COMPOUND_TOP: |