📄 configwidg.3
字号:
\fBmalloc\fR and copying the value into the dynamically-allocatedspace. A pointer to the new string is stored in the target.If TK_CONFIG_NULL_OK is specified in \fIspecFlags\fR then the valuemay be an empty string, in which case the target will be set to NULL.If the previous value of the target wasn't NULL, then it isfreed by passing it to \fBfree\fR..TP\fBTK_CONFIG_SYNONYM\fRThis \fItype\fR value identifies special entries in \fIspecs\fR thatare synonyms for other entries. If an \fIargv\fR value matches the\fIargvName\fR of a TK_CONFIG_SYNONYM entry, the entry isn't useddirectly. Instead, \fBTk_ConfigureWidget\fR searches \fIspecs\fRfor another entry whose \fIargvName\fR is the same as the \fIdbName\fRfield in the TK_CONFIG_SYNONYM entry; this new entry is used justas if its \fIargvName\fR had matched the \fIargv\fR value. Thesynonym mechanism allows multiple \fIargv\fR values to be used fora single configuration option, such as ``\-background'' and ``\-bg''..TP\fBTK_CONFIG_UID\fRThe value is translated to a \fBTk_Uid\fR(by passing it to \fBTk_GetUid\fR). The resulting valueis stored in the target.If TK_CONFIG_NULL_OK is specified in \fIspecFlags\fR and the valueis an empty string then the target will be set to NULL..TP\fBTK_CONFIG_WINDOW\fRThe value must be a window path name. It is translated to a\fBTk_Window\fR token and the token is stored in the target..SH "GROUPED ENTRIES".PPIn some cases it is useful to generate multiple resources froma single configuration value. For example, a color name mightbe used both to generate the background color for a widget (usingTK_CONFIG_COLOR) and to generate a 3-D border to draw around thewidget (using TK_CONFIG_BORDER). In cases like this it is possibleto specify that several consecutive entries in \fIspecs\fR are tobe treated as a group. The first entry is used to determine a value(using its \fIargvName\fR, \fIdbName\fR,\fIdbClass\fR, and \fIdefValue\fR fields). The value will be processedseveral times (one for each entry in the group), generating multipledifferent resources and modifying multiple targets within \fIwidgRec\fR.Each of the entries after the first must have a NULL value in its\fIargvName\fR field; this indicates that the entry is to be groupedwith the entry that precedes it. Only the \fItype\fR and \fIoffset\fRfields are used from these follow-on entries..SH "FLAGS".PPThe \fIflags\fR argument passed to \fBTk_ConfigureWidget\fR is usedin conjunction with the \fIspecFlags\fR fields in the entries of \fIspecs\fRto provide additional control over the processing of configurationoptions. These values are used in three different ways asdescribed below..PPFirst, if the \fIflags\fR argument to \fBTk_ConfigureWidget\fR hasthe TK_CONFIG_ARGV_ONLY bit set (i.e., \fIflags\fR | TK_CONFIG_ARGV_ONLY != 0),then the option database and\fIdefValue\fR fields are not used. In this case, if an entry in\fIspecs\fR doesn't match a field in \fIargv\fR then nothing happens:the corresponding target isn't modified. This feature is usefulwhen the goal is to modify certain configuration options whileleaving others in their current state, such as when a \fBconfigure\fRwidget command is being processed..PPSecond, the \fIspecFlags\fR field of an entry in \fIspecs\fR may be usedto control the processing of that entry. Each \fIspecFlags\fRfield may consists of an OR-ed combination of the following values:.TP\fBTK_CONFIG_COLOR_ONLY\fRIf this bit is set then the entry will only be considered if thedisplay for \fItkwin\fR has more than one bit plane. If the displayis monochromatic then this \fIspecs\fR entry will be ignored..TP\fBTK_CONFIG_MONO_ONLY\fRIf this bit is set then the entry will only be considered if thedisplay for \fItkwin\fR has exactly one bit plane. If the displayis not monochromatic then this \fIspecs\fR entry will be ignored..TP\fBTK_CONFIG_NULL_OK\fRThis bit is only relevant for some types of entries (see thedescriptions of the various entry types above).If this bit is set, it indicates that an empty string valuefor the field is acceptable and if it occurs then thetarget should be set to NULL or \fBNone\fR, dependingon the type of the target.This flag is typically used to allow afeature to be turned off entirely, e.g. set a cursor value to\fBNone\fR so that a window simply inherits its parent's cursor.If this bit isn't set then empty strings are processed as strings,which generally results in an error..TP\fBTK_CONFIG_DONT_SET_DEFAULT\fRIf this bit is one, it means that the \fIdefValue\fR field of theentry should only be used for returning the default value in\fBTk_ConfigureInfo\fR.In calls to \fBTk_ConfigureWidget\fR no default will be suppliedfor entries with this flag set; it is assumed that thecaller has already supplied a default value in the target location.This flag provides a performance optimization where it is expensiveto process the default string: the client can compute the defaultonce, save the value, and provide it before calling\fBTk_ConfigureWidget\fR..TP\fBTK_CONFIG_OPTION_SPECIFIED\fRThis bit is set and cleared by \fBTk_ConfigureWidget\fR. Whenever\fBTk_ConfigureWidget\fR returns, this bit will be set in all theentries where a value was specified in \fIargv\fR.It will be zero in all other entries.This bit provides a way for clients to determine which valuesactually changed in a call to \fBTk_ConfigureWidget\fR..PPThe TK_CONFIG_MONO_ONLY and TK_CONFIG_COLOR_ONLY flags are typicallyused to specify different default values formonochrome and color displays. This is done by creating twoentries in \fIspecs\fR that are identical except for their\fIdefValue\fR and \fIspecFlags\fR fields. One entry should havethe value TK_CONFIG_MONO_ONLY in its \fIspecFlags\fR and thedefault value for monochrome displays in its \fIdefValue\fR; theother entry entry should have the value TK_CONFIG_COLOR_ONLY inits \fIspecFlags\fR and the appropriate \fIdefValue\fR forcolor displays..PPThird, it is possible to use \fIflags\fR and \fIspecFlags\fRtogether to selectively disable some entries. This feature isnot needed very often. It is useful in cases where severalsimilar kinds of widgets are implemented in one place. It allowsa single \fIspecs\fR table to be created with all the configurationoptions for all the widget types. When processing a particularwidget type, only entries relevant to that type will be used. Thiseffect is achieved by setting the high-order bits (those in positionsequal to or greater than TK_CONFIG_USER_BIT) in \fIspecFlags\fRvalues or in \fIflags\fR. In order for a particular entry in\fIspecs\fR to be used, its high-order bits must match exactlythe high-order bits of the \fIflags\fR value passed to\fBTk_ConfigureWidget\fR. If a \fIspecs\fR table is being usedfor N different widget types, then N of the high-order bits willbe used. Each \fIspecs\fR entry will have one of more of thosebits set in its \fIspecFlags\fR field to indicate the widget typesfor which this entry is valid. When calling \fBTk_ConfigureWidget\fR,\fIflags\fR will have a single one of these bits set to select theentries for the desired widget type. For a working example ofthis feature, see the code in tkButton.c..SH TK_OFFSET.PPThe \fBTk_Offset\fR macro is provided as a safe way of generatingthe \fIoffset\fR values for entries in Tk_ConfigSpec structures.It takes two arguments: the name of a type of record, and thename of a field in that record. It returns the byte offset ofthe named field in records of the given type..SH TK_CONFIGUREINFO.PPThe \fBTk_ConfigureInfo\fR procedure may be used to obtaininformation about one or all of the options for a given widget.Given a token for a window (\fItkwin\fR), a table describing theconfiguration options for a class of widgets (\fIspecs\fR), apointer to a widget record containing the current information fora widget (\fIwidgRec\fR), and a NULL \fIargvName\fR argument,\fBTk_ConfigureInfo\fR generates a string describing all of theconfiguration options for the window. The string is placedin \fIinterp->result\fR. Under normal circumstancesit returns TCL_OK; if an error occurs then it returns TCL_ERRORand \fIinterp->result\fR contains an error message..PPIf \fIargvName\fR is NULL, then the value left in\fIinterp->result\fR by \fBTk_ConfigureInfo\fRconsists of a list of one or more entries, each of which describesone configuration option (i.e. one entry in \fIspecs\fR). Eachentry in the list will contain either two or five values. If thecorresponding entry in \fIspecs\fR has type TK_CONFIG_SYNONYM, thenthe list will contain two values: the \fIargvName\fR for the entryand the \fIdbName\fR (synonym name). Otherwise the list will containfive values: \fIargvName\fR, \fIdbName\fR, \fIdbClass\fR, \fIdefValue\fR,and current value. The current value is computed from the appropriatefield of \fIwidgRec\fR by calling procedures like \fBTk_NameOfColor\fR..PPIf the \fIargvName\fR argument to \fBTk_ConfigureInfo\fR is non-NULL,then it indicates a single option, and information is returned onlyfor that option. The string placed in \fIinterp->result\fR will bea list containing two or five values as described above; this willbe identical to the corresponding sublist that would have been returnedif \fIargvName\fR had been NULL..PPThe \fIflags\fR argument to \fBTk_ConfigureInfo\fR is used to restrictthe \fIspecs\fR entries to consider, just as for \fBTk_ConfigureWidget\fR..SH TK_CONFIGUREVALUE.PP\fBTk_ConfigureValue\fR takes arguments similar to \fBTk_ConfigureInfo\fR;instead of returning a list of values, it just returns the current valueof the option given by \fIargvName\fR (\fIargvName\fR must not be NULL).The value is returned in \fIinterp->result\fR and TCL_OK isnormally returned as the procedure's result.If an error occurs in \fBTk_ConfigureValue\fR (e.g., \fIargvName\fR isnot a valid option name), TCL_ERROR is returned and an error messageis left in \fIinterp->result\fR.This procedure is typically called to implement \fBcget\fR widgetcommands..SH TK_FREEOPTIONS.PPThe \fBTk_FreeOptions\fR procedure may be invoked during widget cleanupto release all of the resources associated with configuration options.It scans through \fIspecs\fR and for each entry corresponding to aresource that must be explicitly freed (e.g. those withtype TK_CONFIG_COLOR), it frees the resource in the widget record.If the field in the widget record doesn't refer to a resource (e.g.it contains a null pointer) then no resource is freed for thatentry.After freeing a resource, \fBTk_FreeOptions\fR sets thecorresponding field of the widget record to null..SH "CUSTOM OPTION TYPES".PPApplications can extend the built-in configuration types with additionalconfiguration types by writing procedures to parse and print optionsof the a type and creating a structure pointing to those procedures:.CStypedef struct Tk_CustomOption { Tk_OptionParseProc *\fIparseProc\fR; Tk_OptionPrintProc *\fIprintProc\fR; ClientData \fIclientData\fR;} Tk_CustomOption;typedef int Tk_OptionParseProc( ClientData \fIclientData\fR, Tcl_Interp *\fIinterp\fR, Tk_Window \fItkwin\fR, char *\fIvalue\fR, char *\fIwidgRec\fR, int \fIoffset\fR);typedef char *Tk_OptionPrintProc( ClientData \fIclientData\fR, Tk_Window \fItkwin\fR, char *\fIwidgRec\fR, int \fIoffset\fR, Tcl_FreeProc **\fIfreeProcPtr\fR);.CEThe Tk_CustomOption structure contains three fields, which are pointersto the two procedures and a \fIclientData\fR value to be passed to thoseprocedures when they are invoked. The \fIclientData\fR value typicallypoints to a structure containing information that is needed by theprocedures when they are parsing and printing options..PPThe \fIparseProc\fR procedure is invoked by\fBTk_ConfigureWidget\fR to parse a string and store the resultingvalue in the widget record.The \fIclientData\fR argument is a copy of the \fIclientData\fRfield in the Tk_CustomOption structure.The \fIinterp\fR argument points to a Tcl interpreter used forerror reporting. \fITkwin\fR is a copy of the \fItkwin\fR argumentto \fBTk_ConfigureWidget\fR. The \fIvalue\fR argument is a stringdescribing the value for the option; it could have been specifiedexplicitly in the call to \fBTk_ConfigureWidget\fR or it couldcome from the option database or a default.\fIValue\fR will never be a null pointer but it may point toan empty string.\fIRecordPtr\fR is the same as the \fIwidgRec\fR argument to\fBTk_ConfigureWidget\fR; it points to the start of the widgetrecord to modify.The last argument, \fIoffset\fR, gives the offset in bytes from the startof the widget record to the location where the option value is tobe placed. The procedure should translate the string to whateverform is appropriate for the option and store the value in the widgetrecord. It should normally return TCL_OK, but if an error occursin translating the string to a value then it should return TCL_ERRORand store an error message in \fIinterp->result\fR..PPThe \fIprintProc\fR procedure is calledby \fBTk_ConfigureInfo\fR to produce a string value describing anexisting option.Its \fIclientData\fR, \fItkwin\fR, \fIwidgRec\fR, and \fIoffset\fRarguments all have the same meaning as for Tk_OptionParseProcprocedures.The \fIprintProc\fR procedure should examine the option whose valueis stored at \fIoffset\fR in \fIwidgRec\fR, produce a string describingthat option, and return a pointer to the string.If the string is stored in dynamically-allocated memory, thenthe procedure must set \fI*freeProcPtr\fR to the address ofa procedure to call to free the string's memory; \fBTk_ConfigureInfo\fRwill call this procedure when it is finished with the string.If the result string is stored in static memory then \fIprintProc\fRneed not do anything with the \fIfreeProcPtr\fR argument..PPOnce \fIparseProc\fR and \fIprintProc\fR have been defined and aTk_CustomOption structure has been created for them, options of thisnew type may be manipulated with Tk_ConfigSpec entries whose \fItype\fRfields are TK_CONFIG_CUSTOM and whose \fIcustomPtr\fR fields pointto the Tk_CustomOption structure..SH EXAMPLES.PPAlthough the explanation of \fBTk_ConfigureWidget\fR is fairlycomplicated, its actual use is pretty straightforward.The easiest way to get started is to copy the codefrom an existing widget.The library implementation of frames(tkFrame.c) has a simple configuration table, and the libraryimplementation of buttons (tkButton.c) has a much more complextable that uses many of the fancy \fIspecFlags\fR mechanisms..SH KEYWORDSanchor, bitmap, boolean, border, cap style, color, configuration options,cursor, custom, double, font, integer, join style, justify, millimeters,pixels, relief, synonym, uid
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -