⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tkwinbutton.c

📁 NS2网络仿真软件是目前最为流行的网络仿真模拟软件
💻 C
📖 第 1 页 / 共 3 页
字号:
/*  * tkWinButton.c -- * *	This file implements the Windows specific portion of the button *	widgets. * * Copyright (c) 1996-1998 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. * * RCS: @(#) $Id: tkWinButton.c,v 1.20.2.3 2003/10/10 00:03:57 hobbs Exp $ */#define OEMRESOURCE#include "tkWinInt.h"#include "tkButton.h"/* * These macros define the base style flags for the different button types. */#define LABEL_STYLE (BS_OWNERDRAW | WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS)#define PUSH_STYLE (BS_OWNERDRAW | BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS)#define CHECK_STYLE (BS_OWNERDRAW | BS_CHECKBOX | WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS)#define RADIO_STYLE (BS_OWNERDRAW | BS_RADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS)/* * Declaration of Windows specific button structure. */typedef struct WinButton {    TkButton info;		/* Generic button info. */    WNDPROC oldProc;		/* Old window procedure. */    HWND hwnd;			/* Current window handle. */    Pixmap pixmap;		/* Bitmap for rendering the button. */    DWORD style;		/* Window style flags. */} WinButton;/* * The following macro reverses the order of RGB bytes to convert * between RGBQUAD and COLORREF values. */#define FlipColor(rgb) (RGB(GetBValue(rgb),GetGValue(rgb),GetRValue(rgb)))/* * The following enumeration defines the meaning of the palette entries * in the "buttons" image used to draw checkbox and radiobutton indicators. */enum {    PAL_CHECK = 0,    PAL_TOP_OUTER = 1,    PAL_BOTTOM_OUTER = 2,    PAL_BOTTOM_INNER = 3,    PAL_INTERIOR = 4,    PAL_TOP_INNER = 5,    PAL_BACKGROUND = 6};/* * Cached information about the boxes bitmap, and the default border  * width for a button in string form for use in Tk_OptionSpec for  * the various button widget classes. */typedef struct ThreadSpecificData {     BITMAPINFOHEADER *boxesPtr;   /* Information about the bitmap. */    DWORD *boxesPalette;	  /* Pointer to color palette. */    LPSTR boxesBits;		  /* Pointer to bitmap data. */    DWORD boxHeight;              /* Height of each sub-image. */    DWORD boxWidth ;              /* Width of each sub-image. */    char defWidth[TCL_INTEGER_SPACE];} ThreadSpecificData;static Tcl_ThreadDataKey dataKey;/* * Declarations for functions defined in this file. */static LRESULT CALLBACK	ButtonProc _ANSI_ARGS_((HWND hwnd, UINT message,			    WPARAM wParam, LPARAM lParam));static Window		CreateProc _ANSI_ARGS_((Tk_Window tkwin,			    Window parent, ClientData instanceData));static void		InitBoxes _ANSI_ARGS_((void));/* * The class procedure table for the button widgets. */Tk_ClassProcs tkpButtonProcs = {     sizeof(Tk_ClassProcs),	/* size */    TkButtonWorldChanged,	/* worldChangedProc */    CreateProc,			/* createProc */};/* *---------------------------------------------------------------------- * * InitBoxes -- * *	This function load the Tk 3d button bitmap.  "buttons" is a 16  *	color bitmap that is laid out such that the top row contains  *	the 4 checkbox images, and the bottom row contains the radio  *	button images. Note that the bitmap is stored in bottom-up  *	format.  Also, the first seven palette entries are used to  *	identify the different parts of the bitmaps so we can do the  *	appropriate color mappings based on the current button colors. * * Results: *	None. * * Side effects: *	Loads the "buttons" resource. * *---------------------------------------------------------------------- */static voidInitBoxes(){    /*     * For DLLs like Tk, the HINSTANCE is the same as the HMODULE.     */    HMODULE module = (HINSTANCE) Tk_GetHINSTANCE();    HRSRC hrsrc;    HGLOBAL hblk;    LPBITMAPINFOHEADER newBitmap;    DWORD size;    ThreadSpecificData *tsdPtr = (ThreadSpecificData *)             Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));    hrsrc = FindResource(module, "buttons", RT_BITMAP);    if (hrsrc) {	hblk = LoadResource(module, hrsrc);	tsdPtr->boxesPtr = (LPBITMAPINFOHEADER)LockResource(hblk);    }    /*     * Copy the DIBitmap into writable memory.     */    if (tsdPtr->boxesPtr != NULL && !(tsdPtr->boxesPtr->biWidth % 4)	    && !(tsdPtr->boxesPtr->biHeight % 2)) {	size = tsdPtr->boxesPtr->biSize + (1 << tsdPtr->boxesPtr->biBitCount)                 * sizeof(RGBQUAD) + tsdPtr->boxesPtr->biSizeImage;	newBitmap = (LPBITMAPINFOHEADER) ckalloc(size);	memcpy(newBitmap, tsdPtr->boxesPtr, size);	tsdPtr->boxesPtr = newBitmap;	tsdPtr->boxWidth = tsdPtr->boxesPtr->biWidth / 4;	tsdPtr->boxHeight = tsdPtr->boxesPtr->biHeight / 2;	tsdPtr->boxesPalette = (DWORD*) (((LPSTR) tsdPtr->boxesPtr)                 + tsdPtr->boxesPtr->biSize);	tsdPtr->boxesBits = ((LPSTR) tsdPtr->boxesPalette)	    + ((1 << tsdPtr->boxesPtr->biBitCount) * sizeof(RGBQUAD));    } else {	tsdPtr->boxesPtr = NULL;    }}/* *---------------------------------------------------------------------- * * TkpButtonSetDefaults -- * *	This procedure is invoked before option tables are created for *	buttons.  It modifies some of the default values to match the *	current values defined for this platform. * * Results: *	Some of the default values in *specPtr are modified. * * Side effects: *	Updates some of. * *---------------------------------------------------------------------- */voidTkpButtonSetDefaults(specPtr)    Tk_OptionSpec *specPtr;	/* Points to an array of option specs,				 * terminated by one with type				 * TK_OPTION_END. */{    int width;    ThreadSpecificData *tsdPtr = (ThreadSpecificData *)             Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));    if (tsdPtr->defWidth[0] == 0) {	width = GetSystemMetrics(SM_CXEDGE);	if (width == 0) {	    width = 1;	}	sprintf(tsdPtr->defWidth, "%d", width);    }    for ( ; specPtr->type != TK_OPTION_END; specPtr++) {	if (specPtr->internalOffset == Tk_Offset(TkButton, borderWidth)) {	    specPtr->defValue = tsdPtr->defWidth;	}    }}/* *---------------------------------------------------------------------- * * TkpCreateButton -- * *	Allocate a new TkButton structure. * * Results: *	Returns a newly allocated TkButton structure. * * Side effects: *	Registers an event handler for the widget. * *---------------------------------------------------------------------- */TkButton *TkpCreateButton(tkwin)    Tk_Window tkwin;{    WinButton *butPtr;    butPtr = (WinButton *)ckalloc(sizeof(WinButton));    butPtr->hwnd = NULL;    return (TkButton *) butPtr;}/* *---------------------------------------------------------------------- * * CreateProc -- * *	This function creates a new Button control, subclasses *	the instance, and generates a new Window object. * * Results: *	Returns the newly allocated Window object, or None on failure. * * Side effects: *	Causes a new Button control to come into existence. * *---------------------------------------------------------------------- */static WindowCreateProc(tkwin, parentWin, instanceData)    Tk_Window tkwin;		/* Token for window. */    Window parentWin;		/* Parent of new window. */    ClientData instanceData;	/* Button instance data. */{    Window window;    HWND parent;    char *class;    WinButton *butPtr = (WinButton *)instanceData;    parent = Tk_GetHWND(parentWin);    if (butPtr->info.type == TYPE_LABEL) {	class = "STATIC";	butPtr->style = SS_OWNERDRAW | WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS;    } else {	class = "BUTTON";	butPtr->style = BS_OWNERDRAW | WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS;    }    butPtr->hwnd = CreateWindow(class, NULL, butPtr->style,	    Tk_X(tkwin), Tk_Y(tkwin), Tk_Width(tkwin), Tk_Height(tkwin),	    parent, NULL, Tk_GetHINSTANCE(), NULL);    SetWindowPos(butPtr->hwnd, HWND_TOP, 0, 0, 0, 0,		    SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);#ifdef _WIN64    butPtr->oldProc = (WNDPROC)SetWindowLongPtr(butPtr->hwnd, GWLP_WNDPROC,	    (LONG_PTR) ButtonProc);#else    butPtr->oldProc = (WNDPROC)SetWindowLong(butPtr->hwnd, GWL_WNDPROC,	    (DWORD) ButtonProc);#endif    window = Tk_AttachHWND(tkwin, butPtr->hwnd);    return window;}/* *---------------------------------------------------------------------- * * TkpDestroyButton -- * *	Free data structures associated with the button control. * * Results: *	None. * * Side effects: *	Restores the default control state. * *---------------------------------------------------------------------- */voidTkpDestroyButton(butPtr)    TkButton *butPtr;{    WinButton *winButPtr = (WinButton *)butPtr;    HWND hwnd = winButPtr->hwnd;    if (hwnd) {#ifdef _WIN64	SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) winButPtr->oldProc);#else	SetWindowLong(hwnd, GWL_WNDPROC, (DWORD) winButPtr->oldProc);#endif    }}/* *---------------------------------------------------------------------- * * TkpDisplayButton -- * *	This procedure is invoked to display a button widget.  It is *	normally invoked as an idle handler. * * Results: *	None. * * Side effects: *	Information appears on the screen.  The REDRAW_PENDING flag *	is cleared. * *---------------------------------------------------------------------- */voidTkpDisplayButton(clientData)    ClientData clientData;	/* Information about widget. */{    TkWinDCState state;    HDC dc;    register TkButton *butPtr = (TkButton *) clientData;    GC gc;    Tk_3DBorder border;    Pixmap pixmap;    int x = 0;			/* Initialization only needed to stop				 * compiler warning. */    int y, relief;    register Tk_Window tkwin = butPtr->tkwin;    int width=0, height=0, haveImage = 0, haveText = 0, drawRing = 0;    RECT rect;    int defaultWidth;		/* Width of default ring. */    int offset;			/* 0 means this is a label widget.  1 means				 * it is a flavor of button, so we offset				 * the text to make the button appear to				 * move up and down as the relief changes. */    int textXOffset = 0, textYOffset = 0; /* text offsets for use with					   * compound buttons and focus ring */    int imageWidth=0, imageHeight=0;    int imageXOffset = 0, imageYOffset = 0; /* image information that will					     * be used to restrict disabled					     * pixmap as well */    DWORD *boxesPalette;    ThreadSpecificData *tsdPtr = (ThreadSpecificData *)             Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));    boxesPalette= tsdPtr->boxesPalette;    butPtr->flags &= ~REDRAW_PENDING;    if ((butPtr->tkwin == NULL) || !Tk_IsMapped(tkwin)) {	return;    }    border = butPtr->normalBorder;    if ((butPtr->state == STATE_DISABLED) && (butPtr->disabledFg != NULL)) {	gc = butPtr->disabledGC;    } else if ((butPtr->state == STATE_ACTIVE)	    && !Tk_StrictMotif(butPtr->tkwin)) {	gc = butPtr->activeTextGC;	border = butPtr->activeBorder;    } else {	gc = butPtr->normalTextGC;    }    if ((butPtr->flags & SELECTED) && (butPtr->state != STATE_ACTIVE)	    && (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.  The new     * relief is as follows:     *      If the button is select  --> "sunken"     *      If relief==overrelief    --> relief     *      Otherwise                --> overrelief     *     * The effect we are trying to achieve is as follows:     *     *      value    mouse-over?   -->   relief     *     -------  ------------        --------     *       off        no               flat     *       off        yes              raised     *       on         no               sunken     *       on         yes              sunken     *     * This is accomplished by configuring the checkbutton or radiobutton     * like this:     *     *     -indicatoron 0 -overrelief raised -offrelief flat     *     * Bindings (see library/button.tcl) will copy the -overrelief into

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -