📄 tkimgbmap.c
字号:
/* * tkImgBmap.c -- * * This procedure implements images of type "bitmap" for Tk. * * Copyright (c) 1994 The Regents of the University of California. * Copyright (c) 1994-1997 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: @(#) tkImgBmap.c 1.33 97/07/31 09:08:22 */#include "tkInt.h"#include "tkPort.h"/* * The following data structure represents the master for a bitmap * image: */typedef struct BitmapMaster { Tk_ImageMaster tkMaster; /* Tk's token for image master. NULL means * the image is being deleted. */ Tcl_Interp *interp; /* Interpreter for application that is * using image. */ Tcl_Command imageCmd; /* Token for image command (used to delete * it when the image goes away). NULL means * the image command has already been * deleted. */ int width, height; /* Dimensions of image. */ char *data; /* Data comprising bitmap (suitable for * input to XCreateBitmapFromData). May * be NULL if no data. Malloc'ed. */ char *maskData; /* Data for bitmap's mask (suitable for * input to XCreateBitmapFromData). * Malloc'ed. */ Tk_Uid fgUid; /* Value of -foreground option (malloc'ed). */ Tk_Uid bgUid; /* Value of -background option (malloc'ed). */ char *fileString; /* Value of -file option (malloc'ed). */ char *dataString; /* Value of -data option (malloc'ed). */ char *maskFileString; /* Value of -maskfile option (malloc'ed). */ char *maskDataString; /* Value of -maskdata option (malloc'ed). */ struct BitmapInstance *instancePtr; /* First in list of all instances associated * with this master. */} BitmapMaster;/* * The following data structure represents all of the instances of an * image that lie within a particular window: */typedef struct BitmapInstance { int refCount; /* Number of instances that share this * data structure. */ BitmapMaster *masterPtr; /* Pointer to master for image. */ Tk_Window tkwin; /* Window in which the instances will be * displayed. */ XColor *fg; /* Foreground color for displaying image. */ XColor *bg; /* Background color for displaying image. */ Pixmap bitmap; /* The bitmap to display. */ Pixmap mask; /* Mask: only display bitmap pixels where * there are 1's here. */ GC gc; /* Graphics context for displaying bitmap. * None means there was an error while * setting up the instance, so it cannot * be displayed. */ struct BitmapInstance *nextPtr; /* Next in list of all instance structures * associated with masterPtr (NULL means * end of list). */} BitmapInstance;/* * The type record for bitmap images: */static int GetByte _ANSI_ARGS_((Tcl_Channel chan));static int ImgBmapCreate _ANSI_ARGS_((Tcl_Interp *interp, char *name, int argc, char **argv, Tk_ImageType *typePtr, Tk_ImageMaster master, ClientData *clientDataPtr));static ClientData ImgBmapGet _ANSI_ARGS_((Tk_Window tkwin, ClientData clientData));static void ImgBmapDisplay _ANSI_ARGS_((ClientData clientData, Display *display, Drawable drawable, int imageX, int imageY, int width, int height, int drawableX, int drawableY));static void ImgBmapFree _ANSI_ARGS_((ClientData clientData, Display *display));static void ImgBmapDelete _ANSI_ARGS_((ClientData clientData));Tk_ImageType tkBitmapImageType = { "bitmap", /* name */ ImgBmapCreate, /* createProc */ ImgBmapGet, /* getProc */ ImgBmapDisplay, /* displayProc */ ImgBmapFree, /* freeProc */ ImgBmapDelete, /* deleteProc */ (Tk_ImageType *) NULL /* nextPtr */};/* * Information used for parsing configuration specs: */static Tk_ConfigSpec configSpecs[] = { {TK_CONFIG_UID, "-background", (char *) NULL, (char *) NULL, "", Tk_Offset(BitmapMaster, bgUid), 0}, {TK_CONFIG_STRING, "-data", (char *) NULL, (char *) NULL, (char *) NULL, Tk_Offset(BitmapMaster, dataString), TK_CONFIG_NULL_OK}, {TK_CONFIG_STRING, "-file", (char *) NULL, (char *) NULL, (char *) NULL, Tk_Offset(BitmapMaster, fileString), TK_CONFIG_NULL_OK}, {TK_CONFIG_UID, "-foreground", (char *) NULL, (char *) NULL, "#000000", Tk_Offset(BitmapMaster, fgUid), 0}, {TK_CONFIG_STRING, "-maskdata", (char *) NULL, (char *) NULL, (char *) NULL, Tk_Offset(BitmapMaster, maskDataString), TK_CONFIG_NULL_OK}, {TK_CONFIG_STRING, "-maskfile", (char *) NULL, (char *) NULL, (char *) NULL, Tk_Offset(BitmapMaster, maskFileString), TK_CONFIG_NULL_OK}, {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL, (char *) NULL, 0, 0}};/* * The following data structure is used to describe the state of * parsing a bitmap file or string. It is used for communication * between TkGetBitmapData and NextBitmapWord. */#define MAX_WORD_LENGTH 100typedef struct ParseInfo { char *string; /* Next character of string data for bitmap, * or NULL if bitmap is being read from * file. */ Tcl_Channel chan; /* File containing bitmap data, or NULL * if no file. */ char word[MAX_WORD_LENGTH+1]; /* Current word of bitmap data, NULL * terminated. */ int wordLength; /* Number of non-NULL bytes in word. */} ParseInfo;/* * Prototypes for procedures used only locally in this file: */static int ImgBmapCmd _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp, int argc, char **argv));static void ImgBmapCmdDeletedProc _ANSI_ARGS_(( ClientData clientData));static void ImgBmapConfigureInstance _ANSI_ARGS_(( BitmapInstance *instancePtr));static int ImgBmapConfigureMaster _ANSI_ARGS_(( BitmapMaster *masterPtr, int argc, char **argv, int flags));static int NextBitmapWord _ANSI_ARGS_((ParseInfo *parseInfoPtr));/* *---------------------------------------------------------------------- * * ImgBmapCreate -- * * This procedure is called by the Tk image code to create "test" * images. * * Results: * A standard Tcl result. * * Side effects: * The data structure for a new image is allocated. * *---------------------------------------------------------------------- */ /* ARGSUSED */static intImgBmapCreate(interp, name, argc, argv, typePtr, master, clientDataPtr) Tcl_Interp *interp; /* Interpreter for application containing * image. */ char *name; /* Name to use for image. */ int argc; /* Number of arguments. */ char **argv; /* Argument strings for options (doesn't * include image name or type). */ Tk_ImageType *typePtr; /* Pointer to our type record (not used). */ Tk_ImageMaster master; /* Token for image, to be used by us in * later callbacks. */ ClientData *clientDataPtr; /* Store manager's token for image here; * it will be returned in later callbacks. */{ BitmapMaster *masterPtr; masterPtr = (BitmapMaster *) ckalloc(sizeof(BitmapMaster)); masterPtr->tkMaster = master; masterPtr->interp = interp; masterPtr->imageCmd = Tcl_CreateCommand(interp, name, ImgBmapCmd, (ClientData) masterPtr, ImgBmapCmdDeletedProc); masterPtr->width = masterPtr->height = 0; masterPtr->data = NULL; masterPtr->maskData = NULL; masterPtr->fgUid = NULL; masterPtr->bgUid = NULL; masterPtr->fileString = NULL; masterPtr->dataString = NULL; masterPtr->maskFileString = NULL; masterPtr->maskDataString = NULL; masterPtr->instancePtr = NULL; if (ImgBmapConfigureMaster(masterPtr, argc, argv, 0) != TCL_OK) { ImgBmapDelete((ClientData) masterPtr); return TCL_ERROR; } *clientDataPtr = (ClientData) masterPtr; return TCL_OK;}/* *---------------------------------------------------------------------- * * ImgBmapConfigureMaster -- * * This procedure is called when a bitmap image is created or * reconfigured. It process configuration options and resets * any instances of the image. * * Results: * A standard Tcl return value. If TCL_ERROR is returned then * an error message is left in masterPtr->interp->result. * * Side effects: * Existing instances of the image will be redisplayed to match * the new configuration options. * *---------------------------------------------------------------------- */static intImgBmapConfigureMaster(masterPtr, argc, argv, flags) BitmapMaster *masterPtr; /* Pointer to data structure describing * overall bitmap image to (reconfigure). */ int argc; /* Number of entries in argv. */ char **argv; /* Pairs of configuration options for image. */ int flags; /* Flags to pass to Tk_ConfigureWidget, * such as TK_CONFIG_ARGV_ONLY. */{ BitmapInstance *instancePtr; int maskWidth, maskHeight, dummy1, dummy2; if (Tk_ConfigureWidget(masterPtr->interp, Tk_MainWindow(masterPtr->interp), configSpecs, argc, argv, (char *) masterPtr, flags) != TCL_OK) { return TCL_ERROR; } /* * Parse the bitmap and/or mask to create binary data. Make sure that * the bitmap and mask have the same dimensions. */ if (masterPtr->data != NULL) { ckfree(masterPtr->data); masterPtr->data = NULL; } if ((masterPtr->fileString != NULL) || (masterPtr->dataString != NULL)) { masterPtr->data = TkGetBitmapData(masterPtr->interp, masterPtr->dataString, masterPtr->fileString, &masterPtr->width, &masterPtr->height, &dummy1, &dummy2); if (masterPtr->data == NULL) { return TCL_ERROR; } } if (masterPtr->maskData != NULL) { ckfree(masterPtr->maskData); masterPtr->maskData = NULL; } if ((masterPtr->maskFileString != NULL) || (masterPtr->maskDataString != NULL)) { if (masterPtr->data == NULL) { masterPtr->interp->result = "can't have mask without bitmap"; return TCL_ERROR; } masterPtr->maskData = TkGetBitmapData(masterPtr->interp, masterPtr->maskDataString, masterPtr->maskFileString, &maskWidth, &maskHeight, &dummy1, &dummy2); if (masterPtr->maskData == NULL) { return TCL_ERROR; } if ((maskWidth != masterPtr->width) || (maskHeight != masterPtr->height)) { ckfree(masterPtr->maskData); masterPtr->maskData = NULL; masterPtr->interp->result = "bitmap and mask have different sizes"; return TCL_ERROR; } } /* * Cycle through all of the instances of this image, regenerating * the information for each instance. Then force the image to be * redisplayed everywhere that it is used. */ for (instancePtr = masterPtr->instancePtr; instancePtr != NULL; instancePtr = instancePtr->nextPtr) { ImgBmapConfigureInstance(instancePtr); } Tk_ImageChanged(masterPtr->tkMaster, 0, 0, masterPtr->width, masterPtr->height, masterPtr->width, masterPtr->height); return TCL_OK;}/* *---------------------------------------------------------------------- * * ImgBmapConfigureInstance -- * * This procedure is called to create displaying information for * a bitmap image instance based on the configuration information * in the master. It is invoked both when new instances are * created and when the master is reconfigured. * * Results: * None. * * Side effects: * Generates errors via Tcl_BackgroundError if there are problems * in setting up the instance. * *---------------------------------------------------------------------- */static voidImgBmapConfigureInstance(instancePtr) BitmapInstance *instancePtr; /* Instance to reconfigure. */{ BitmapMaster *masterPtr = instancePtr->masterPtr; XColor *colorPtr; XGCValues gcValues; GC gc; unsigned int mask; /* * For each of the options in masterPtr, translate the string * form into an internal form appropriate for instancePtr. */ if (*masterPtr->bgUid != 0) { colorPtr = Tk_GetColor(masterPtr->interp, instancePtr->tkwin, masterPtr->bgUid); if (colorPtr == NULL) { goto error; } } else { colorPtr = NULL; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -