Tk_ConfigureWidget(3tk)
β¨ NAME β¨
Tk_ConfigureWidget(3tk) β Tk Library Procedures π«Ά
Okay bestie, gather round: Tk_ConfigureWidget, Tk_ConfigureInfo, Tk_ConfigureValue, and Tk_FreeOptions β they process configuration options for widgets. That's the whole name section, periodt. π
πΈ SYNOPSIS πΈ
#include <tk.h>
int
Tk_ConfigureWidget(interp, tkwin, specs, objc, objv, widgRec, flags)
int
Tk_ConfigureInfo(interp, tkwin, specs, widgRec, argvName, flags)
int
Tk_ConfigureValue(interp, tkwin, specs, widgRec, argvName, flags)
Tk_FreeOptions(specs, widgRec, display, flags)
π ARGUMENTS π
Tcl_Interp *interp(in) β the interpreter you use for returning error messages. It's giving "where the drama goes if something breaks" πTk_Window tkwin(in) β the window used to represent the widget. Needed to set up X resources, no cap. πconst Tk_ConfigSpec *specs(in) β pointer to the table specifying legal configuration options for this widget. This is the master list, bestie. πTcl_Size objc(in) β number of arguments inobjv. Count 'em. π’Tcl_Obj *const * objv(in) β command-line options for configuring the widget. The actual argv energy. β¨char *widgRec(in/out) β points to the widget record structure. Fields in this structure get modified byTk_ConfigureWidgetto hold configuration information. So it's not just a spectator, it gets edited. πint flags(in) β if non-zero, then it specifies an OR-ed combination of flags that control the processing of configuration information.TK_CONFIG_ARGV_ONLYcauses the option database and defaults to be ignored, and flag bitsTK_CONFIG_USER_BITand higher are used to selectively disable entries inspecs. Big brain flag stuff. π§type name type(in) β the name of the type of a widget record. Yes, that's literally how it's written. πfield name field(in) β the name of a field in records of typetype. Okayyy. πconst char *argvName(in) β the name used on Tcl command lines to refer to a particular option (e.g. when creating a widget or invoking theconfigurewidget command). If non-NULL, then information is returned only for this option. IfNULL, then information is returned for all available options. SoNULL= give me everything, bestie. πDisplay *display(in) β display containing widget whose record is being freed; needed in order to free up resources. Cleanup era. π§Ή
π DESCRIPTION π
β οΈ Note thatTk_ConfigureWidgetshould be replaced with the new Tcl_Obj based API,Tk_SetOptions. The old interface is retained for backward compatibility. So yes, it's legacy, but we still love her. π
Tk_ConfigureWidget is called to configure various aspects of a widget, such as colors, fonts, border width, etc. It is intended as a convenience procedure to reduce the amount of code that must be written in individual widget managers to handle configuration information. It is typically invoked when widgets are created, and again when the configure command is invoked for a widget. Although intended primarily for widgets, Tk_ConfigureWidget can be used in other situations where objc-objv information is to be used to fill in a record structure, such as configuring graphical elements for a canvas widget or entries of a menu. So it's not just widget-coded, it's record-filling coded. π«Ά
Tk_ConfigureWidget processes a table specifying the configuration options that are supported (specs) and a collection of command-line arguments (objc and objv) to fill in fields of a record (widgRec). It uses the option database and defaults specified in specs to fill in fields of widgRec that are not specified in objv. Tk_ConfigureWidget normally returns the value TCL_OK; in this case it does not modify interp. If an error occurs then TCL_ERROR is returned and Tk_ConfigureWidget will leave an error message in interpreter interp's result in the standard Tcl fashion. In the event of an error return, some of the fields of widgRec could already have been set, if configuration information for them was successfully processed before the error occurred. The other fields will be set to reasonable initial values so that Tk_FreeOptions can be called for cleanup. So yeah, partial progress is a thing, but cleanup is still possible. ππ
The specs array specifies the kinds of configuration options expected by the widget. Each of its entries specifies one configuration option and has the following structure:
typedef struct {
int type;
const char *argvName;
const char *dbName;
const char *dbClass;
const char *defValue;
Tcl_Size offset;
int specFlags;
const Tk_CustomOption *customPtr;
} Tk_ConfigSpec;
The type field indicates what type of configuration option this is (e.g. TK_CONFIG_COLOR for a color value, or TK_CONFIG_INT for an integer value). The type field indicates how to use the value of the option (more on this below). The argvName field is a string such as β-fontβ or β-bgβ, which is compared with the values in objv (if argvName is NULL it means this is a grouped entry; see GROUPED ENTRIES below). The dbName and dbClass fields are used to look up a value for this option in the option database. The defValue field specifies a default value for this configuration option if no value is specified in either objv or the option database. Offset indicates where in widgRec to store information about this option, and specFlags contains additional information to control the processing of this configuration option (see FLAGS below). The last field, customPtr, is only used if type is TK_CONFIG_CUSTOM; see CUSTOM OPTION TYPES below. Iconic struct behavior. β¨
Tk_ConfigureWidget first processes objv to see which (if any) configuration options are specified there. Argv must contain an even number of fields; the first of each pair of fields must match the argvName of some entry in specs (unique abbreviations are acceptable), and the second field of the pair contains the value for that configuration option. If there are entries in spec for which there were no matching entries in objv, Tk_ConfigureWidget uses the dbName and dbClass fields of the specs entry to probe the option database; if a value is found, then it is used as the value for the option. Finally, if no entry is found in the option database, the defValue field of the specs entry is used as the value for the configuration option. If the defValue is NULL, or if the TK_CONFIG_DONT_SET_DEFAULT bit is set in flags, then there is no default value and this specs entry will be ignored if no value is specified in objv or the option database. So defaults are not always defaulting, bestie. π
Once a string value has been determined for a configuration option, Tk_ConfigureWidget translates the string value into a more useful form, such as a color if type is TK_CONFIG_COLOR or an integer if type is TK_CONFIG_INT. This value is then stored in the record pointed to by widgRec. This record is assumed to contain information relevant to the manager of the widget; its exact type is unknown to Tk_ConfigureWidget. The offset field of each specs entry indicates where in widgRec to store the information about this configuration option. You should use the offsetof macro to generate offset values. The location indicated by widgRec and offset will be referred to as the βtargetβ in the descriptions below. Target acquired, bestie. π―
The type field of each entry in specs determines what to do with the string value of that configuration option. The legal values for type, and the corresponding actions, are:
TK_CONFIG_ACTIVE_CURSORβ The value must be an ASCII string identifying a cursor in a form suitable for passing toTk_GetCursor. The value is converted to aTk_Cursorby callingTk_GetCursorand the result is stored in the target. In addition, the resulting cursor is made the active cursor fortkwinby callingXDefineCursor. IfTK_CONFIG_NULL_OKis specified inspecFlagsthen the value may be an empty string, in which case the target andtkwin's active cursor will be set toNone. If the previous value of the target was notNone, then it is freed by passing it toTk_FreeCursor. Cursor drama, but make it active. π±οΈβ¨TK_CONFIG_ANCHORβ The value must be an ASCII string identifying an anchor point in one of the ways accepted byTk_GetAnchor. The string is converted to aTk_Anchorby callingTk_GetAnchorand the result is stored in the target. Anchor it down. βTK_CONFIG_BITMAPβ The value must be an ASCII string identifying a bitmap in a form suitable for passing toTk_GetBitmap. The value is converted to aPixmapby callingTk_GetBitmapand the result is stored in the target. IfTK_CONFIG_NULL_OKis specified inspecFlagsthen the value may be an empty string, in which case the target is set toNone. If the previous value of the target was notNone, then it is freed by passing it toTk_FreeBitmap. Bitmap bestie. π¨TK_CONFIG_BOOLEANβ The value must be an ASCII string specifying a boolean value. Any of the values"true","yes","on", or"1", or an abbreviation of one of these values, means true; any of the values"false","no","off", or"0", or an abbreviation of one of these values, means false. The target is expected to be an integer; for true values it will be set to1and for false values it will be set to0. Boolean slay. β βTK_CONFIG_BORDERβ The value must be an ASCII string identifying a border color in a form suitable for passing toTk_Get3DBorder. The value is converted to a(Tk_3DBorder *)by callingTk_Get3DBorderand the result is stored in the target. IfTK_CONFIG_NULL_OKis specified inspecFlagsthen the value may be an empty string, in which case the target will be set toNULL. If the previous value of the target was notNULL, then it is freed by passing it toTk_Free3DBorder. Border? More like baddie. πTK_CONFIG_CAP_STYLEβ The value must be an ASCII string identifying a cap style in one of the ways accepted byTk_GetCapStyle. The string is converted to an integer value corresponding to the cap style by callingTk_GetCapStyleand the result is stored in the target. Cap style, cap vibes. π§’TK_CONFIG_COLORβ bestie, this one's a color. The value has to be an ASCII string that names a color in a form you could hand straight toTk_GetColor. We convert it by callingTk_GetColor, get back an(XColor *), and stash that in the target. IfTK_CONFIG_NULL_OKis set inspecFlags, an empty string is allowed and the target gets set toNone. And if the target's previous value wasn'tNULL, we free it by passing it toTk_FreeColor. πTK_CONFIG_CURSORβ literally identical toTK_CONFIG_ACTIVE_CURSORexcept the new cursor does not get made the active one fortkwin. That's it, that's the difference. πTK_CONFIG_CUSTOMβ the one where apps get to invent their own option types, iconic behavior honestly. ThecustomPtrfield of the entry points at a structure defining the new option type. Full deets live in the CUSTOM OPTION TYPES section below. π¦TK_CONFIG_DOUBLEβ the value has to be an ASCII floating-point number in the formatstrtolaccepts. The string gets converted to adoublevalue, and that value lands in the target. β¨TK_CONFIG_ENDβ this marks the end of the table, periodt. The last entry inspecsmust have this type; all its other fields are ignored and it will never match any arguments. πTK_CONFIG_FONTβ the value has to be an ASCII string naming a font in a form suitable for passing toTk_GetFont. We convert it by callingTk_GetFont, get aTk_Font, and store that in the target. IfTK_CONFIG_NULL_OKis set inspecFlags, an empty string is fine and the target gets set toNULL. If the target's previous value wasn'tNULL, we free it by passing it toTk_FreeFont. π«ΆTK_CONFIG_INTβ the value must be an ASCII integer string in the formatstrtolaccepts (so yes,β0βandβ0xβprefixes can be used to specify octal or hexadecimal numbers, respectively). The string gets converted to an integer value and the integer goes in the target. π₯TK_CONFIG_JOIN_STYLEβ the value must be an ASCII string identifying a join style in one of the waysTk_GetJoinStyleaccepts. The string is converted to an integer value corresponding to the join style by callingTk_GetJoinStyle, and that result goes in the target. πTK_CONFIG_JUSTIFYβ the value must be an ASCII string identifying a justification method in one of the waysTk_GetJustifyaccepts. The string is converted to aTk_Justifyby callingTk_GetJustify, and the result is stored in the target. βTK_CONFIG_MMβ the value must specify a screen distance in one of the formsTk_GetScreenMMaccepts. The string is converted to a double-precision floating-point distance in millimeters, and that value goes in the target. πTK_CONFIG_PIXELSβ the value must specify screen units in one of the formsTk_GetPixelsaccepts. The string is converted to an integer distance in pixels, and the value goes in the target. πΈTK_CONFIG_RELIEFβ the value must be an ASCII string identifying a relief in a form suitable for passing toTk_GetRelief. The value is converted to an integer relief value by callingTk_GetRelief, and the result is stored in the target. β¨TK_CONFIG_STRINGβ a copy of the value gets made by allocating memory space withTcl_Allocand copying the value into that dynamically-allocated space. A pointer to the new string is stored in the target. IfTK_CONFIG_NULL_OKis set inspecFlags, an empty string is allowed and the target gets set toNULL. If the target's previous value wasn'tNULL, it's freed by passing it toTcl_Free. π (memory management bestie, it's giving responsibility)TK_CONFIG_SYNONYMβ this type value identifies special entries inspecsthat are synonyms for other entries. If anobjvvalue matches theargvNameof aTK_CONFIG_SYNONYMentry, that entry is not used directly. Instead,Tk_ConfigureWidgetsearchesspecsfor another entry whoseargvNameis the same as thedbNamefield in theTK_CONFIG_SYNONYMentry; this new entry is used just as if itsargvNamehad matched theobjvvalue. The synonym mechanism lets multipleobjvvalues be used for a single configuration option, likeβ-backgroundβandβ-bgβ. πTK_CONFIG_UIDβ the value is translated to aTk_Uid(by passing it toTk_GetUid). The resulting value is stored in the target. IfTK_CONFIG_NULL_OKis set inspecFlagsand the value is an empty string, then the target gets set toNULL. π¦TK_CONFIG_WINDOWβ the value must be a window path name. It's translated to aTk_Windowtoken and the token is stored in the target. π
π GROUPED ENTRIES π
Okay so sometimes it's genuinely useful to generate multiple resources from a single configuration value. Like, a color name might be used both to generate the background color for a widget (using TK_CONFIG_COLOR) and to generate a 3-D border to draw around the widget (using TK_CONFIG_BORDER). Two resources, one vibe. π
In cases like this, you can specify that several consecutive entries in specs are treated as a group. The first entry determines a value (using its argvName, dbName, dbClass, and defValue fields). That value then gets processed several times β once for each entry in the group β generating multiple different resources and modifying multiple targets within widgRec. π₯
Each of the entries after the first must have a NULL value in its argvName field; that's the signal that the entry is grouped with the entry that precedes it. And from these follow-on entries, only the type and offset fields are used. That's the whole rule, no cap. π
π© FLAGS π©
The flags argument passed to Tk_ConfigureWidget works together with the specFlags fields in the entries of specs to give you extra control over how configuration options get processed. These values are used in three different ways, bestie. Let me break it down. β¨
First up: if the flags argument to Tk_ConfigureWidget has the TK_CONFIG_ARGV_ONLY bit set (i.e., flags | TK_CONFIG_ARGV_ONLY != 0), then the option database and defValue fields are not used. In this case, if an entry in specs doesn't match a field in objv then nothing happens: the corresponding target is not modified. This feature is useful when the goal is to modify certain configuration options while leaving others in their current state β like when a configure widget command is being processed. π
Second: the specFlags field of an entry in specs can be used to control the processing of that entry. Each specFlags field can consist of an OR-ed combination of the following values: π¦
TK_CONFIG_COLOR_ONLYβ if this bit is set, the entry is only considered if the display fortkwinhas more than one bit plane. If the display is monochromatic, then thisspecsentry gets ignored. πTK_CONFIG_MONO_ONLYβ if this bit is set, the entry is only considered if the display fortkwinhas exactly one bit plane. If the display is not monochromatic, then thisspecsentry gets ignored. πTK_CONFIG_NULL_OKβ this bit is only relevant for some types of entries (peep the descriptions of the various entry types above). If this bit is set, it means an empty string value for the field is acceptable, and if one shows up then the target should be set toNULLorNone, depending on the type of the target. This flag is typically used to let a feature be turned off entirely β e.g. set a cursor value toNoneso a window just inherits its parent's cursor. If this bit is not set, then empty strings get processed as strings, which generally results in an error. πTK_CONFIG_DONT_SET_DEFAULTβ if this bit is one, it means thedefValuefield of the entry should only be used for returning the default value inTk_ConfigureInfo. In calls toTk_ConfigureWidget, no default will be supplied for entries with this flag set; it's assumed the caller already supplied a default value in the target location. This flag is a performance optimization for when it's expensive to process the default string: the client can compute the default once, save the value, and provide it before callingTk_ConfigureWidget. Efficiency queen behavior honestly. π
The TK_CONFIG_MONO_ONLY and TK_CONFIG_COLOR_ONLY flags are typically used to specify different default values for monochrome and color displays. How? By creating two entries in specs that are identical except for their defValue and specFlags fields. One entry should have the value TK_CONFIG_MONO_ONLY in its specFlags and the default value for monochrome displays in its defValue; the other entry should have the value TK_CONFIG_COLOR_ONLY in its specFlags and the appropriate defValue for color displays. π
Third: you can use flags and specFlags together to selectively disable some entries. This feature isn't needed very often, ngl. It's useful when several similar kinds of widgets are implemented in one place, because it lets a single specs table be created with all the configuration options for all the widget types. When processing a particular widget type, only the entries relevant to that type get used. π
How's that achieved? By setting the high-order bits (those in positions equal to or greater than TK_CONFIG_USER_BIT) in specFlags values or in flags. For a particular entry in specs to be used, its high-order bits must match exactly the high-order bits of the flags value passed to Tk_ConfigureWidget. If a specs table is being used for N different widget types, then N of the high-order bits get used. Each specs entry will have one or more of those bits set in its specFlags field to indicate the widget types for which this entry is valid. When calling Tk_ConfigureWidget, flags will have a single one of these bits set to select the entries for the desired widget type. For a working example of this feature, see the code in tkButton.c. π₯
π§ TK_CONFIGUREINFO
okay bestie, so Tk_ConfigureInfo is the little detective π that goes and finds out stuff about a widget's options β one of them, or literally all of them, your choice, we love options.
So you hand it:
- a token for a window (
tkwin) - a table describing the configuration options for a class of widgets (
specs) - a pointer to a widget record with the current info for a widget (
widgRec) - and an
argvNameargument that'sNULL
β¦and it generates a string describing all the configuration options for the window. That string goes into interpreter interp's result. Under normal circumstances it returns TCL_OK; but if an error happens, it returns TCL_ERROR and the interpreter's result will hold an error message. π
If argvName is NULL, then the value left in the interpreter's result by Tk_ConfigureInfo is a list of one or more entries, and each one describes one configuration option (i.e. one entry in specs). Every entry in that list will contain either two or five values:
- if the corresponding entry in
specshas typeTK_CONFIG_SYNONYM, the list has two values: theargvNamefor the entry and thedbName(synonym name) β¨ - otherwise the list has five values:
argvName,dbName,dbClass,defValue, and current value
The current value gets computed from the appropriate field of widgRec by calling procedures like Tk_NameOfColor. iconic.
But wait β if the argvName argument to Tk_ConfigureInfo is non-NULL, then it's pointing at a single option, and you only get info back for that one option. The string in the interpreter's result will be a list with two or five values as described above, and it will be identical to the corresponding sublist that would've been returned if argvName had been NULL. no cap.
And the flags argument to Tk_ConfigureInfo is used to restrict the specs entries to consider, just as for Tk_ConfigureWidget. π
π TK_CONFIGUREVALUE
Tk_ConfigureValue takes arguments similar to Tk_ConfigureInfo, but instead of giving you a whole list of values, it just returns the current value of the option given by argvName β and heads up, argvName must not be NULL. π¨
The value is returned in interpreter interp's result and TCL_OK is normally returned as the procedure's result. But if an error occurs in Tk_ConfigureValue (like, say, argvName isn't a valid option name), then TCL_ERROR is returned and an error message is left in the interpreter's result. π
This procedure is typically called to implement cget widget commands. slay.
π§Ή TK_FREEOPTIONS
Tk_FreeOptions is your cleanup bestie β invoke it during widget cleanup to release all the resources associated with configuration options. π«Ά
It scans through specs and, for each entry corresponding to a resource that must be explicitly freed (e.g. those with type 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 nothing gets freed for that entry, no stress. And after freeing a resource, Tk_FreeOptions sets the corresponding field of the widget record to null. clean. β¨
π¦ CUSTOM OPTION TYPES
Okay this part is the DIY section and it's lowkey the coolest. Applications can extend the built-in configuration types with extra configuration types by writing procedures to parse and print options of the a type and creating a structure pointing to those procedures:
typedef struct {
Tk_OptionParseProc *parseProc;
Tk_OptionPrintProc *printProc;
void *clientData;
} Tk_CustomOption;
typedef int Tk_OptionParseProc(
void *clientData,
Tcl_Interp *interp,
Tk_Window tkwin,
const char *value,
char *widgRec,
Tcl_Size offset);
typedef const char *Tk_OptionPrintProc(
void *clientData,
Tk_Window tkwin,
char *widgRec,
Tcl_Size offset,
Tcl_FreeProc **freeProcPtr);
The Tk_CustomOption structure contains three fields, which are pointers to the two procedures and a clientData value to be passed to those procedures when they get invoked. The clientData value typically points to a structure containing info that the procedures need when they're parsing and printing options. π
πΈ the parseProc situation
The parseProc procedure is invoked by Tk_ConfigureWidget to parse a string and store the resulting value in the widget record. Let's break down who's who:
clientDataβ a copy of theclientDatafield in theTk_CustomOptionstructureinterpβ points to a Tcl interpreter used for error reportingTkwinβ a copy of thetkwinargument toTk_ConfigureWidgetvalueβ a string describing the value for the option; it could've been specified explicitly in the call toTk_ConfigureWidget, or it could come from the option database or a default.Valuewill never be a null pointer but it may point to an empty string. πRecordPtrβ the same as thewidgRecargument toTk_ConfigureWidget; it points to the start of the widget record to modifyoffsetβ the last argument, gives the offset in bytes from the start of the widget record to the location where the option value is to be placed
The procedure should translate the string to whatever form is appropriate for the option and store the value in the widget record. It should normally return TCL_OK, but if an error occurs translating the string to a value then it should return TCL_ERROR and store an error message in interpreter interp's result. π€
π the printProc situation
The printProc procedure is called by Tk_ConfigureInfo to produce a string value describing an existing option. Its clientData, tkwin, widgRec, and offset arguments all have the same meaning as for Tk_OptionParseProc procedures.
The printProc procedure should examine the option whose value is stored at offset in widgRec, produce a string describing that option, and return a pointer to the string. And here's the important bit: if the string is stored in dynamically-allocated memory, then the procedure must set *freeProcPtr to the address of a procedure to call to free the string's memory β Tk_ConfigureInfo will call this procedure when it's finished with the string. If the result string is stored in static memory then printProc need not do anything with the freeProcPtr argument. easy β¨
π putting it together
Once parseProc and printProc have been defined and a Tk_CustomOption structure has been created for them, options of this new type may be manipulated with Tk_ConfigSpec entries whose type fields are TK_CONFIG_CUSTOM and whose customPtr fields point to the Tk_CustomOption structure. periodt. π
π EXAMPLES
Although the explanation of Tk_ConfigureWidget is fairly complicated, its actual use is pretty straightforward, bestie, I promise. π«Ά
The easiest way to get started is to copy the code from an existing widget:
- the library implementation of frames (
tkFrame.c) has a simple configuration table π - and the library implementation of buttons (
tkButton.c) has a much more complex table that uses many of the fancyspecFlagsmechanisms π₯
π SEE ALSO
β KEYWORDS
anchor, bitmap, boolean, border, cap style, color, configuration options, cursor, custom, double, font, integer, join style, justify, millimeters, pixels, relief, synonym, uid
okay that's the whole thing, we made it, you're officially a Tk_ConfigureWidget girlie now π
β¨ this page comes to you from the Tk 4.1 era and lives under the name Tk_ConfigureWidget(3tk) β iconic behavior, honestly, and yes you should absolutely go read Tk_SetOptions(3tk) next. mwah πππ¦
π¦ where this page comes from
- debian trixie: tk9.0-doc 9.0.1-3
- ubuntu questing: tk9.0-doc 9.0.2-1
- ubuntu resolute: tk9.0-doc 9.0.3-1
- ubuntu stonking: tk9.0-doc 9.0.4-1
- Copyright (c) 1990-1994 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms