📄 crtitemtype.3
字号:
'\"'\" Copyright (c) 1994-1995 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: @(#) CrtItemType.3 1.7 96/02/16 10:30:28'\" .so man.macros.TH Tk_CreateItemType 3 4.0 Tk "Tk Library Procedures".BS.SH NAMETk_CreateItemType, Tk_GetItemTypes \- define new kind of canvas item.SH SYNOPSIS.nf\fB#include <tk.h>\fR.sp\fBTk_CreateItemType\fR(\fItypePtr\fR).spTk_ItemType *\fBTk_GetItemTypes\fR().SH ARGUMENTS.AS Tk_ItemType *typePtr.AP Tk_ItemType *typePtr inStructure that defines the new type of canvas item..BE.SH INTRODUCTION.PP\fBTk_CreateItemType\fR is invoked to define a new kind of canvas itemdescribed by the \fItypePtr\fR argument.An item type corresponds to a particular value of the \fItype\fRargument to the \fBcreate\fR widget command for canvases, andthe code that implements a canvas item type is called a \fItype manager\fR.Tk defines several built-in item types, such as \fBrectangle\fRand \fBtext\fR and \fBimage\fR, but \fBTk_CreateItemType\fRallows additional item types to be defined.Once \fBTk_CreateItemType\fR returns, the new item type may be usedin new or existing canvas widgets just like the built-in itemtypes..PP\fBTk_GetItemTypes\fR returns a pointer to the first in the listof all item types currently defined for canvases.The entries in the list are linked together through their\fInextPtr\fR fields, with the end of the list marked by aNULL \fInextPtr\fR..PPYou may find it easier to understand the rest of this manual entryby looking at the code for an existing canvas item type such asbitmap (file tkCanvBmap.c) or text (tkCanvText.c).The easiest way to create a new type manager is to copy the codefor an existing type and modify it for the new type..PPTk provides a number of utility procedures for the use of canvastype managers, such as \fBTk_CanvasCoords\fR and \fBTk_CanvasPsColor\fR;these are described in separate manual entries..SH "DATA STRUCTURES".PPA type manager consists of a collection of procedures that provide astandard set of operations on items of that type.The type manager deals with three kinds of datastructures.The first data structure is a Tk_ItemType; it containsinformation such as the name of the type and pointers tothe standard procedures implemented by the type manager:.CStypedef struct Tk_ItemType { char *\fIname\fR; int \fIitemSize\fR; Tk_ItemCreateProc *\fIcreateProc\fR; Tk_ConfigSpec *\fIconfigSpecs\fR; Tk_ItemConfigureProc *\fIconfigProc\fR; Tk_ItemCoordProc *\fIcoordProc\fR; Tk_ItemDeleteProc *\fIdeleteProc\fR; Tk_ItemDisplayProc *\fIdisplayProc\fR; int \fIalwaysRedraw\fR; Tk_ItemPointProc *\fIpointProc\fR; Tk_ItemAreaProc *\fIareaProc\fR; Tk_ItemPostscriptProc *\fIpostscriptProc\fR; Tk_ItemScaleProc *\fIscaleProc\fR; Tk_ItemTranslateProc *\fItranslateProc\fR; Tk_ItemIndexProc *\fIindexProc\fR; Tk_ItemCursorProc *\fIicursorProc\fR; Tk_ItemSelectionProc *\fIselectionProc\fR; Tk_ItemInsertProc *\fIinsertProc\fR; Tk_ItemDCharsProc *\fIdCharsProc\fR; Tk_ItemType *\fInextPtr\fR;} Tk_ItemType;.CE.PPThe fields of a Tk_ItemType structure are described in more detaillater in this manual entry.When \fBTk_CreateItemType\fR is called, its \fItypePtr\fRargument must point to a structure with all of the fields initializedexcept \fInextPtr\fR, which Tk sets to link all the types togetherinto a list.The structure must be in permanent memory (either staticallyallocated or dynamically allocated but never freed); Tk retainsa pointer to this structure..PPThe second data structure manipulated by a type manager is an\fIitem record\fR.For each item in a canvas there exists one item record.All of the items of a given type generally have item records withthe same structure, but different types usually have differentformats for their item records.The first part of each item record is a header with a standard structuredefined by Tk via the type Tk_Item; the rest of the itemrecord is defined by the type manager.A type manager must define its item records with a Tk_Item asthe first field.For example, the item record for bitmap items is defined as follows:.CStypedef struct BitmapItem { Tk_Item \fIheader\fR; double \fIx\fR, \fIy\fR; Tk_Anchor \fIanchor\fR; Pixmap \fIbitmap\fR; XColor *\fIfgColor\fR; XColor *\fIbgColor\fR; GC \fIgc\fR;} BitmapItem;.CEThe \fIheader\fR substructure contains information used by Tkto manage the item, such as its identifier, its tags, its type,and its bounding box.The fields starting with \fIx\fR belong to the type manager:Tk will never read or write them.The type manager should not need to read or write any of thefields in the header except for four fieldswhose names are \fIx1\fR, \fIy1\fR, \fIx2\fR, and \fIy2\fR.These fields give a bounding box for the items using integercanvas coordinates: the item should not cover any pixelswith x-coordinate lower than \fIx1\fR or y-coordinatelower than \fIy1\fR, nor should it cover any pixels withx-coordinate greater than or equal to \fIx2\fR or y-coordinategreater than or equal to \fIy2\fR.It is up to the type manager to keep the bounding box up todate as the item is moved and reconfigured..PPWhenever Tk calls a procedure in a type manager it passes in a pointerto an item record.The argument is always passed as a pointer to a Tk_Item; the typemanager will typically cast this into a pointer to its own specifictype, such as BitmapItem..PPThe third data structure used by type managers has typeTk_Canvas; it serves as an opaque handle for the canvas widgetas a whole.Type managers need not know anything about the contents of thisstructure.A Tk_Canvas handle is typically passed in to theprocedures of a type manager, and the type manager can pass thehandle back to library procedures such as Tk_CanvasTkwinto fetch information about the canvas..SH NAME.PPThis section and the ones that follow describe each of the fieldsin a Tk_ItemType structure in detail.The \fIname\fR field provides a string name for the item type.Once \fBTk_CreateImageType\fR returns, this name may be usedin \fBcreate\fR widget commands to create items of the newtype.If there already existed an item type by this name thenthe new item type replaces the old one..SH ITEMSIZE\fItypePtr->itemSize\fR gives the size in bytes of item recordsof this type, including the Tk_Item header.Tk uses this size to allocate memory space for items of the type.All of the item records for a given type must have the same size.If variable length fields are needed for an item (such as a listof points for a polygon), the type manager can allocate a separateobject of variable length and keep a pointer to it in the item record..SH CREATEPROC.PP\fItypePtr->createProc\fR points to a procedure forTk to call whenever a new item of this type is created.\fItypePtr->createProc\fR must match the following prototype:.CStypedef int Tk_ItemCreateProc( Tcl_Interp *\fIinterp\fR, Tk_Canvas \fIcanvas\fR, Tk_Item *\fIitemPtr\fR, int \fIargc\fR, char **\fIargv\fR);.CEThe \fIinterp\fR argument is the interpreter in which the canvas's\fBcreate\fR widget command was invoked, and \fIcanvas\fR is ahandle for the canvas widget.\fIitemPtr\fR is a pointer to a newly-allocated item ofsize \fItypePtr->itemSize\fR.Tk has already initialized the item's header (the first\fBsizeof(Tk_ItemType)\fR bytes).The \fIargc\fR and \fIargv\fR arguments describe all of thearguments to the \fBcreate\fR command after the \fItype\fRargument.For example, in the widget command.CS\fB\&.c create rectangle 10 20 50 50 \-fill black\fR.CE\fIargc\fR will be \fB6\fR and \fIargv\fR[0] will contain thestring \fB10\fR..PP\fIcreateProc\fR should use \fIargc\fR and \fIargv\fR to initializethe type-specific parts of the item record and set an initial valuefor the bounding box in the item's header.It should return a standard Tcl completion code and leave anerror message in \fIinterp->result\fR if an error occurs.If an error occurs Tk will free the item record, so \fIcreateProc\fRmust be sure to leave the item record in a clean state if it returns an error(e.g., it must free any additional memory that it allocated forthe item)..SH CONFIGSPECS.PPEach type manager must provide a standard table describing itsconfiguration options, in a form suitable for use with\fBTk_ConfigureWidget\fR.This table will normally be used by \fItypePtr->createProc\fRand \fItypePtr->configProc\fR, but Tk also uses it directlyto retrieve option information in the \fBitemcget\fR and\fBitemconfigure\fR widget commands.\fItypePtr->configSpecs\fR must point to the configuration tablefor this type.Note: Tk provides a custom option type \fBtk_CanvasTagsOption\fRfor implementing the \fB\-tags\fR option; see an existing typemanager for an example of how to use it in \fIconfigSpecs\fR..SH CONFIGPROC.PP\fItypePtr->configProc\fR is called by Tk whenever the\fBitemconfigure\fR widget command is invoked to change theconfiguration options for a canvas item.This procedure must match the following prototype:.CStypedef int Tk_ItemConfigureProc( Tcl_Interp *\fIinterp\fR, Tk_Canvas \fIcanvas\fR, Tk_Item *\fIitemPtr\fR, int \fIargc\fR, char **\fIargv\fR, int \fIflags\fR);.CEThe \fIinterp\fR argument identifies the interpreter in which thewidget command was invoked, \fIcanvas\fR is a handle for the canvaswidget, and \fIitemPtr\fR is a pointer to the item being configured.\fIargc\fR and \fIargv\fR contain the configuration options. Forexample, if the following command is invoked:.CS\fB\&.c itemconfigure 2 \-fill red \-outline black\fR.CE\fIargc\fR is \fB4\fR and \fIargv\fR contains the strings \fB\-fill\fRthrough \fBblack\fR.\fIargc\fR will always be an even value.The \fIflags\fR argument contains flags to pass to \fBTk_ConfigureWidget\fR;currently this value is always TK_CONFIG_ARGV_ONLY when Tkinvokes \fItypePtr->configProc\fR, but the type manager's \fIcreateProc\fRprocedure will usually invoke \fIconfigProc\fR with different flag values..PP\fItypePtr->configProc\fR returns a standard Tcl completion code andleaves an error message in \fIinterp->result\fR if an error occurs.It must update the item's bounding box to reflect the new configurationoptions..SH COORDPROC.PP\fItypePtr->coordProc\fR is invoked by Tk to implement the \fBcoords\fRwidget command for an item.It must match the following prototype:.CStypedef int Tk_ItemCoordProc( Tcl_Interp *\fIinterp\fR, Tk_Canvas \fIcanvas\fR, Tk_Item *\fIitemPtr\fR, int \fIargc\fR, char **\fIargv\fR);.CEThe arguments \fIinterp\fR, \fIcanvas\fR, and \fIitemPtr\fRall have the standard meanings, and \fIargc\fR and \fIargv\fRdescribe the coordinate arguments.For example, if the following widget command is invoked:.CS\fB\&.c coords 2 30 90\fR.CE\fIargc\fR will be \fB2\fR and \fBargv\fR will contain the string values\fB30\fR and \fB90\fR..PPThe \fIcoordProc\fR procedure should process the new coordinates,update the item appropriately (e.g., it must reset the boundingbox in the item's header), and return a standard Tcl completioncode.If an error occurs, \fIcoordProc\fR must leave an error message in\fIinterp->result\fR..SH DELETEPROC.PP\fItypePtr->deleteProc\fR is invoked by Tk to delete an itemand free any resources allocated to it.It must match the following prototype:.CStypedef void Tk_ItemDeleteProc( Tk_Canvas \fIcanvas\fR, Tk_Item *\fIitemPtr\fR, Display *\fIdisplay\fR);.CEThe \fIcanvas\fR and \fIitemPtr\fR arguments have the usualinterpretations, and \fIdisplay\fR identifies the X display containingthe canvas.\fIdeleteProc\fR must free up any resources allocated for the item,so that Tk can free the item record.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -