📄 tkmacbutton.c
字号:
/* * tkMacButton.c -- * * This file implements the Macintosh specific portion of the * button widgets. * * Copyright (c) 1996-1997 by Sun Microsystems, Inc. * * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * * SCCS: @(#) tkMacButton.c 1.18 97/11/20 18:27:21 */#include "tkButton.h"#include "tkMacInt.h"#include <Controls.h>#include <LowMem.h>#include <Appearance.h>#include <ToolUtils.h>/* * Some defines used to control what type of control is drawn. */#define DRAW_LABEL 0 /* Labels are treated genericly. */#define DRAW_CONTROL 1 /* Draw using the Native control. */#define DRAW_CUSTOM 2 /* Make our own button drawing. */#define DRAW_BEVEL 3/* * The following structures are used to draw our controls. Rather than * having many Mac controls we just use one control of each type and * reuse them for all Tk widgets. When the windowRef variable is NULL * it means none of the data structures have been allocated. */static WindowRef windowRef = NULL;static CWindowRecord windowRecord;static ControlRef buttonHandle;static ControlRef checkHandle;static ControlRef radioHandle;static ControlRef smallBevelHandle;static ControlRef smallStickyBevelHandle;static ControlRef medBevelHandle;static ControlRef medStickyBevelHandle;static ControlRef largeBevelHandle;static ControlRef largeStickyBevelHandle;/* * These are used to store the image content for * beveled buttons - i.e. buttons with images. */ static ControlButtonContentInfo bevelButtonContent;static OpenCPicParams picParams;static CCTabHandle buttonTabHandle;static CCTabHandle checkTabHandle;static CCTabHandle radioTabHandle;static PixMapHandle oldPixPtr;/* * These functions are used when Appearance is present. * By embedding all our controls in a userPane control, * we can color the background of the text in radiobuttons * and checkbuttons. Thanks to Peter Gontier of Apple DTS * for help on this one. */static ControlRef userPaneHandle;static RGBColor gUserPaneBackground = { ~0, ~0, ~0};static pascal OSErr SetUserPaneDrawProc(ControlRef control, ControlUserPaneDrawProcPtr upp);static pascal OSErr SetUserPaneSetUpSpecialBackgroundProc(ControlRef control, ControlUserPaneBackgroundProcPtr upp);static pascal void UserPaneDraw(ControlRef control, ControlPartCode cpc);static pascal void UserPaneBackgroundProc(ControlHandle, ControlBackgroundPtr info);/* * Forward declarations for procedures defined later in this file: */static int UpdateControlColors _ANSI_ARGS_((TkButton *butPtr, ControlRef controlHandle, CCTabHandle ccTabHandle, RGBColor *saveColorPtr));static void DrawBufferedControl _ANSI_ARGS_((TkButton *butPtr, GWorldPtr destPort, GC gc, Pixmap pixmap));static void InitSampleControls();static void SetupBevelButton _ANSI_ARGS_((TkButton *butPtr, ControlRef controlHandle, GWorldPtr destPort, GC gc, Pixmap pixmap));static void ChangeBackgroundWindowColor _ANSI_ARGS_(( WindowRef macintoshWindow, RGBColor rgbColor, RGBColor *oldColor));static void ButtonExitProc _ANSI_ARGS_((ClientData clientData));/* * The class procedure table for the button widgets. */TkClassProcs tkpButtonProcs = { NULL, /* createProc. */ TkButtonWorldChanged, /* geometryProc. */ NULL /* modalProc. */};/* *---------------------------------------------------------------------- * * TkpCreateButton -- * * Allocate a new TkButton structure. * * Results: * Returns a newly allocated TkButton structure. * * Side effects: * Registers an event handler for the widget. * *---------------------------------------------------------------------- */TkButton *TkpCreateButton( Tk_Window tkwin){ return (TkButton *) ckalloc(sizeof(TkButton));}/* *---------------------------------------------------------------------- * * TkpDisplayButton -- * * This procedure is invoked to display a button widget. It is * normally invoked as an idle handler. * * Results: * None. * * Side effects: * Commands are output to X to display the button in its * current mode. The REDRAW_PENDING flag is cleared. * *---------------------------------------------------------------------- */voidTkpDisplayButton( ClientData clientData) /* Information about widget. */{ TkButton *butPtr = (TkButton *) clientData; Pixmap pixmap; GC gc; Tk_3DBorder border; int x = 0; /* Initialization only needed to stop * compiler warning. */ int y, relief; register Tk_Window tkwin = butPtr->tkwin; int width, height; int offset; /* 0 means this is a normal widget. 1 means * it is an image button, so we offset the * image to make the button appear to move * up and down as the relief changes. */ int hasImageOrBitmap; CGrafPtr saveWorld; GDHandle saveDevice; GWorldPtr destPort; int drawType, borderWidth; GetGWorld(&saveWorld, &saveDevice); butPtr->flags &= ~REDRAW_PENDING; if ((butPtr->tkwin == NULL) || !Tk_IsMapped(tkwin)) { return; } /* * In order to avoid screen flashes, this procedure redraws * the button in a pixmap, then copies the pixmap to the * screen in a single operation. This means that there's no * point in time where the on-sreen image has been cleared. */ pixmap = Tk_GetPixmap(butPtr->display, Tk_WindowId(tkwin), Tk_Width(tkwin), Tk_Height(tkwin), Tk_Depth(tkwin)); hasImageOrBitmap = ((butPtr->image != NULL) || (butPtr->bitmap != None)); offset = (butPtr->type == TYPE_BUTTON) && hasImageOrBitmap; border = butPtr->normalBorder; if ((butPtr->state == tkDisabledUid) && (butPtr->disabledFg != NULL)) { gc = butPtr->disabledGC; } else if ((butPtr->type == TYPE_BUTTON) && (butPtr->state == tkActiveUid)) { gc = butPtr->activeTextGC; border = butPtr->activeBorder; } else { gc = butPtr->normalTextGC; } if ((butPtr->flags & SELECTED) && (butPtr->state != tkActiveUid) && (butPtr->selectBorder != NULL) && !butPtr->indicatorOn) { border = butPtr->selectBorder; } /* * Override the relief specified for the button if this is a * checkbutton or radiobutton and there's no indicator. * However, don't do this in the presence of Appearance, since * then the bevel button will take care of the relief. */ relief = butPtr->relief; if ((butPtr->type >= TYPE_CHECK_BUTTON) && !butPtr->indicatorOn) { if (!TkMacHaveAppearance() || !hasImageOrBitmap) { relief = (butPtr->flags & SELECTED) ? TK_RELIEF_SUNKEN : TK_RELIEF_RAISED; } } /* * See the comment in UpdateControlColors as to why we use the * highlightbackground for the border of Macintosh buttons. */ if (butPtr->type == TYPE_BUTTON) { Tk_Fill3DRectangle(tkwin, pixmap, butPtr->highlightBorder, 0, 0, Tk_Width(tkwin), Tk_Height(tkwin), 0, TK_RELIEF_FLAT); } else { Tk_Fill3DRectangle(tkwin, pixmap, butPtr->normalBorder, 0, 0, Tk_Width(tkwin), Tk_Height(tkwin), 0, TK_RELIEF_FLAT); } if (butPtr->type == TYPE_LABEL) { drawType = DRAW_LABEL; } else if (butPtr->type == TYPE_BUTTON) { if (!hasImageOrBitmap) { drawType = DRAW_CONTROL; } else if (butPtr->image != None) { drawType = DRAW_BEVEL; } else { /* * TO DO - The current way the we draw bitmaps (XCopyPlane) * uses CopyDeepMask in this one case. The Picture recording * does not record this call, and so we can't use the * Appearance bevel button here. The only case that would * exercise this is if you use a bitmap, with * -data & -mask specified. We should probably draw the * appearance button and overprint the image in this case. * This just punts and draws the old-style, ugly, button. */ if (gc->clip_mask == 0) { drawType = DRAW_BEVEL; } else { TkpClipMask *clipPtr = (TkpClipMask*) gc->clip_mask; if ((clipPtr->type == TKP_CLIP_PIXMAP) && (clipPtr->value.pixmap != butPtr->bitmap)) { drawType = DRAW_CUSTOM; } else { drawType = DRAW_BEVEL; } } } } else { if (butPtr->indicatorOn) { drawType = DRAW_CONTROL; } else if (hasImageOrBitmap) { if (gc->clip_mask == 0) { drawType = DRAW_BEVEL; } else { TkpClipMask *clipPtr = (TkpClipMask*) gc->clip_mask; if ((clipPtr->type == TKP_CLIP_PIXMAP) && (clipPtr->value.pixmap != butPtr->bitmap)) { drawType = DRAW_CUSTOM; } else { drawType = DRAW_BEVEL; } } } else { drawType = DRAW_CUSTOM; } } /* * Draw the native portion of the buttons. Start by creating the control * if it doesn't already exist. Then configure the Macintosh control from * the Tk info. Finally, we call Draw1Control to draw to the screen. */ if ((drawType == DRAW_CONTROL) || ((drawType == DRAW_BEVEL) && TkMacHaveAppearance())) { borderWidth = 0; /* * This part uses Macintosh rather than Tk calls to draw * to the screen. Make sure the ports etc. are set correctly. */ destPort = TkMacGetDrawablePort(pixmap); SetGWorld(destPort, NULL); DrawBufferedControl(butPtr, destPort, gc, pixmap); } if ((drawType == DRAW_CUSTOM) || (drawType == DRAW_LABEL)) { borderWidth = butPtr->borderWidth; } /* * Display image or bitmap or text for button. This has * already been done under Appearance with the Bevel * button types. */ if ((drawType == DRAW_BEVEL) && TkMacHaveAppearance()) { /* Empty Body */ } else if (butPtr->image != None) { Tk_SizeOfImage(butPtr->image, &width, &height); imageOrBitmap: TkComputeAnchor(butPtr->anchor, tkwin, 0, 0, butPtr->indicatorSpace + width, height, &x, &y); x += butPtr->indicatorSpace; x += offset; y += offset; if (relief == TK_RELIEF_RAISED) { x -= offset; y -= offset; } else if (relief == TK_RELIEF_SUNKEN) { x += offset; y += offset; } if (butPtr->image != NULL) { if ((butPtr->selectImage != NULL) && (butPtr->flags & SELECTED)) { Tk_RedrawImage(butPtr->selectImage, 0, 0, width, height, pixmap, x, y); } else { Tk_RedrawImage(butPtr->image, 0, 0, width, height, pixmap, x, y); } } else { XSetClipOrigin(butPtr->display, gc, x, y); XCopyPlane(butPtr->display, butPtr->bitmap, pixmap, gc, 0, 0, (unsigned int) width, (unsigned int) height, x, y, 1); XSetClipOrigin(butPtr->display, gc, 0, 0); } y += height/2; } else if (butPtr->bitmap != None) { Tk_SizeOfBitmap(butPtr->display, butPtr->bitmap, &width, &height); goto imageOrBitmap; } else { TkComputeAnchor(butPtr->anchor, tkwin, butPtr->padX, butPtr->padY, butPtr->indicatorSpace + butPtr->textWidth, butPtr->textHeight, &x, &y); x += butPtr->indicatorSpace; Tk_DrawTextLayout(butPtr->display, pixmap, gc, butPtr->textLayout, x, y, 0, -1); y += butPtr->textHeight/2; } /* * If the button is disabled with a stipple rather than a special * foreground color, generate the stippled effect. If the widget * is selected and we use a different background color when selected, * must temporarily modify the GC. */ if ((butPtr->state == tkDisabledUid) && ((butPtr->disabledFg == NULL) || (butPtr->image != NULL))) { if ((butPtr->flags & SELECTED) && !butPtr->indicatorOn && (butPtr->selectBorder != NULL)) { XSetForeground(butPtr->display, butPtr->disabledGC, Tk_3DBorderColor(butPtr->selectBorder)->pixel); } XFillRectangle(butPtr->display, pixmap, butPtr->disabledGC, butPtr->inset, butPtr->inset, (unsigned) (Tk_Width(tkwin) - 2*butPtr->inset), (unsigned) (Tk_Height(tkwin) - 2*butPtr->inset)); if ((butPtr->flags & SELECTED) && !butPtr->indicatorOn && (butPtr->selectBorder != NULL)) { XSetForeground(butPtr->display, butPtr->disabledGC, Tk_3DBorderColor(butPtr->normalBorder)->pixel); } } /* * Draw the border and traversal highlight last. This way, if the * button's contents overflow they'll be covered up by the border. */ if (relief != TK_RELIEF_FLAT) { int inset = butPtr->highlightWidth; Tk_Draw3DRectangle(tkwin, pixmap, border, inset, inset, Tk_Width(tkwin) - 2*inset, Tk_Height(tkwin) - 2*inset, butPtr->borderWidth, relief); } /* * Copy the information from the off-screen pixmap onto the screen, * then delete the pixmap. */ XCopyArea(butPtr->display, pixmap, Tk_WindowId(tkwin), butPtr->copyGC, 0, 0, (unsigned) Tk_Width(tkwin), (unsigned) Tk_Height(tkwin), 0, 0); Tk_FreePixmap(butPtr->display, pixmap); SetGWorld(saveWorld, saveDevice);}/* *---------------------------------------------------------------------- * * TkpComputeButtonGeometry -- * * After changes in a button's text or bitmap, this procedure * recomputes the button's geometry and passes this information * along to the geometry manager for the window. * * Results: * None. * * Side effects: * The button's window may change size. * *---------------------------------------------------------------------- */voidTkpComputeButtonGeometry( TkButton *butPtr) /* Button whose geometry may have changed. */{ int width, height, avgWidth; Tk_FontMetrics fm; /* * First figure out the size of the contents of the button. */ butPtr->indicatorSpace = 0; if (butPtr->image != NULL) { Tk_SizeOfImage(butPtr->image, &width, &height); imageOrBitmap: if (butPtr->width > 0) { width = butPtr->width; } if (butPtr->height > 0) { height = butPtr->height; } if ((butPtr->type >= TYPE_CHECK_BUTTON) && butPtr->indicatorOn) { butPtr->indicatorSpace = height; if (butPtr->type == TYPE_CHECK_BUTTON) { butPtr->indicatorDiameter = (65*height)/100; } else { butPtr->indicatorDiameter = (75*height)/100; } } } else if (butPtr->bitmap != None) { Tk_SizeOfBitmap(butPtr->display, butPtr->bitmap, &width, &height); goto imageOrBitmap; } else { Tk_FreeTextLayout(butPtr->textLayout); butPtr->textLayout = Tk_ComputeTextLayout(butPtr->tkfont, butPtr->text, -1, butPtr->wrapLength, butPtr->justify, 0, &butPtr->textWidth, &butPtr->textHeight); width = butPtr->textWidth; height = butPtr->textHeight; avgWidth = Tk_TextWidth(butPtr->tkfont, "0", 1); Tk_GetFontMetrics(butPtr->tkfont, &fm); if (butPtr->width > 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -