Tk_AllocBitmapFromObj(3)
πΈ Tk_AllocBitmapFromObj(3) πΈ
a.k.a. your fave Tk Library Procedures era β bitmap database management, but make it cute π
β¨ NAME β¨
Okay bestie, roll call: Tk_AllocBitmapFromObj, Tk_GetBitmap, Tk_GetBitmapFromObj, Tk_DefineBitmap, Tk_NameOfBitmap, Tk_SizeOfBitmap, Tk_FreeBitmapFromObj, Tk_FreeBitmap β maintain database of single-plane pixmaps π
β¨ SYNOPSIS β¨
No cap, here's the exact spellbook: πͺ
#include <tk.h>
Pixmap
Tk_AllocBitmapFromObj(interp, tkwin, objPtr)
Pixmap
Tk_GetBitmap(interp, tkwin, info)
Pixmap
Tk_GetBitmapFromObj(tkwin, objPtr)
int
Tk_DefineBitmap(interp, name, source, width, height)
const char *
Tk_NameOfBitmap(display, bitmap)
Tk_SizeOfBitmap(display, bitmap, widthPtr, heightPtr)
Tk_FreeBitmapFromObj(tkwin, objPtr)
Tk_FreeBitmap(display, bitmap)
β¨ ARGUMENTS β¨
Here's the cast, bestie β every single one matters: π«Ά
Tcl_Interp *interp(in)β Interpreter to use for error reporting; ifNULLthen no error message is left after errors.Tk_Window tkwin(in)β Token for window in which the bitmap will be used.Tcl_Obj *objPtr(in/out)β String value describes desired bitmap; internal rep will be modified to cache pointer to correspondingPixmap.const char *info(in)β Same asobjPtrexcept description of bitmap is passed as a string and resultingPixmapis not cached.const char *name(in)β Name for new bitmap to be defined.const void *source(in)β Data for bitmap, in standard bitmap format. Must be stored in static memory whose value will never change.int width(in)β Width of bitmap.int height(in)β Height of bitmap.int *widthPtr(out)β Pointer to word to fill in with bitmap's width.int *heightPtr(out)β Pointer to word to fill in with bitmap's height.Display *display(in)β Display for which bitmap was allocated.Pixmap bitmap(in)β Identifier for a bitmap allocated byTk_AllocBitmapFromObjorTk_GetBitmap.
β¨ DESCRIPTION β¨
So these procedures are literally managing a whole collection of bitmaps β one-plane pixmaps, to be exact β that an application is using. β¨ The whole point is that bitmaps get re-used efficiently, which avoids server overhead, and they can also be named with character strings. Iconic behavior, honestly. π
Tk_AllocBitmapFromObj returns a Pixmap identifier for a bitmap that matches the description in objPtr and is suitable for use in tkwin. It re-uses an existing bitmap if it can, and creates a new one otherwise. objPtr's value must have one of these forms:
@fileNameβfileNamemust be the name of a file containing a bitmap description in the standard X11 format.nameβnamemust be the name of a bitmap defined previously with a call toTk_DefineBitmap.
The following names are pre-defined by Tk: π
errorβ The international βdon'tβ symbol: a circle with a diagonal line across it.gray75β 75% gray: a checkerboard pattern where three out of four bits are on.gray50β 50% gray: a checkerboard pattern where every other bit is on.gray25β 25% gray: a checkerboard pattern where one out of every four bits is on.gray12β 12.5% gray: a pattern where one-eighth of the bits are on, consisting of every fourth pixel in every other row.hourglassβ An hourglass symbol.infoβ A large letter βiβ.questheadβ The silhouette of a human head, with a question mark in it.questionβ A large question-mark.warningβ A large exclamation point.
In addition, the following pre-defined names are available only on the Macintosh platform: π
documentβ A generic document.stationeryβ Document stationery.editionβ The edition symbol.applicationβ Generic application icon.accessoryβ A desk accessory.folderβ Generic folder icon.pfolderβ A locked folder.trashβ A trash can.floppyβ A floppy disk.ramdiskβ A floppy disk with chip.cdromβ A cd disk icon.preferencesβ A folder with prefs symbol.querydocβ A database document icon.stopβ A stop sign.noteβ A face with balloon words.cautionβ A triangle with an exclamation point.
Under normal conditions, Tk_AllocBitmapFromObj returns an identifier for the requested bitmap. But bestie, if an error occurs while creating the bitmap β like when objPtr refers to a non-existent file β then None is returned and an error message is left in interp's result if interp is not NULL. π Tk_AllocBitmapFromObj caches information about the return value in objPtr, which speeds up future calls to procedures such as Tk_AllocBitmapFromObj and Tk_GetBitmapFromObj.
Tk_GetBitmap is identical to Tk_AllocBitmapFromObj except that the description of the bitmap is specified with a string instead of an object. This prevents Tk_GetBitmap from caching the return value, so Tk_GetBitmap is less efficient than Tk_AllocBitmapFromObj. Not me pointing out the efficiency drop, but facts are facts. π
Tk_GetBitmapFromObj returns the token for an existing bitmap, given the window and description used to create the bitmap. Tk_GetBitmapFromObj does not actually create the bitmap; the bitmap must already have been created with a previous call to Tk_AllocBitmapFromObj or Tk_GetBitmap. The return value is cached in objPtr, which speeds up future calls to Tk_GetBitmapFromObj with the same objPtr and tkwin.
Tk_DefineBitmap associates a name with in-memory bitmap data so that the name can be used in later calls to Tk_AllocBitmapFromObj or Tk_GetBitmap. The nameId argument gives a name for the bitmap; it must not previously have been used in a call to Tk_DefineBitmap. The arguments source, width, and height describe the bitmap. Tk_DefineBitmap normally returns TCL_OK; if an error occurs (e.g. a bitmap named nameId has already been defined) then TCL_ERROR is returned and an error message is left in interpreter interp's result.
Important caveat, bestie:Tk_DefineBitmapexpects the memory pointed to bysourceto be static:Tk_DefineBitmapdoes not make a private copy of this memory, but uses the bytes pointed to bysourcelater in calls toTk_AllocBitmapFromObjorTk_GetBitmap. Do not come for this static-memory rule, it's serious. π«Ά
Typically Tk_DefineBitmap is used by #include-ing a bitmap file directly into a C program and then referencing the variables defined by the file. For example, suppose there exists a file stip.bitmap, which was created by the bitmap program and contains a stipple pattern. The following code uses Tk_DefineBitmap to define a new bitmap named foo:
Pixmap bitmap;
#include "stip.bitmap"
Tk_DefineBitmap(interp, "foo", stip_bits,
stip_width, stip_height);
...
bitmap = Tk_GetBitmap(interp, tkwin, "foo");
This code causes the bitmap file to be read at compile-time and incorporates the bitmap information into the program's executable image. The same bitmap file could be read at run-time using Tk_GetBitmap:
Pixmap bitmap;
bitmap = Tk_GetBitmap(interp, tkwin, "@stip.bitmap");
The second form is a bit more flexible (the file could be modified after the program has been compiled, or a different string could be provided to read a different file), but it is a little slower and requires the bitmap file to exist separately from the program. Trade-offs, bestie β iconic but occasionally inconvenient. β¨
Tk maintains a database of all the bitmaps that are currently in use. Whenever possible, it will return an existing bitmap rather than creating a new one. When a bitmap is no longer used, Tk will release it automatically. This approach can substantially reduce server overhead, so Tk_AllocBitmapFromObj and Tk_GetBitmap should generally be used in preference to Xlib procedures like XReadBitmapFile. Slay the server overhead, periodt. π
The bitmaps returned by Tk_AllocBitmapFromObj and Tk_GetBitmap are shared, so callers should never modify them. If a bitmap must be modified dynamically, then it should be created by calling Xlib procedures such as XReadBitmapFile or XCreatePixmap directly. Shared means hands off, bestie β no unauthorized edits. π
The procedure Tk_NameOfBitmap is roughly the inverse of Tk_GetBitmap. Given an X Pixmap argument, it returns the textual description that was passed to Tk_GetBitmap when the bitmap was created. bitmap must have been the return value from a previous call to Tk_AllocBitmapFromObj or Tk_GetBitmap.
Tk_SizeOfBitmap returns the dimensions of its bitmap argument in the words pointed to by the widthPtr and heightPtr arguments. As with Tk_NameOfBitmap, bitmap must have been created by Tk_AllocBitmapFromObj or Tk_GetBitmap.
When a bitmap is no longer needed, Tk_FreeBitmapFromObj or Tk_FreeBitmap should be called to release it. For Tk_FreeBitmapFromObj the bitmap to release is specified with the same information used to create it; for Tk_FreeBitmap the bitmap to release is specified with its Pixmap token. There should be exactly one call to Tk_FreeBitmapFromObj or Tk_FreeBitmap for each call to Tk_AllocBitmapFromObj or Tk_GetBitmap. Cleanup math: one free for every alloc/get. Don't leave your bitmaps in the group chat unread. π
π BUGS π
okay so bestie, this is the part where we talk about the β¨quirksβ¨ β and no, I'm not gonna pretend they're not there π
When Tk_AllocBitmapFromObj and Tk_GetBitmap are trying to figure out whether an existing bitmap can be used to satisfy a new request, they look at one thing and one thing only: the immediate value of the string description. That's it. That's the whole vibe check πΈ
So like, picture this π«Ά β you pass a file name to Tk_GetBitmap, and it'll be like "oh, I already have a bitmap made from that exact same file name, we're so good, re-using it π
". But bestie... it does not check whether the file itself has changed π. It does not check whether the current directory has changed either. And if the current directory did change, that same name could totally be pointing at a completely different file now, and it would have zero clue. Not me finding out the hard way π
lowkey important: this means stale bitmaps can absolutely sneak back in and you'd never know. The name matched, so it assumed everything matched. periodt π
π KEYWORDS π
bitmappixmap
and that's the whole page, bestie!! you made it all the way through, iconic behavior honestly πβ¨ go forth and allocate responsibly π¦
source: Tk 8.1 Β· Tk_AllocBitmapFromObj(3) β signed, sealed, delivered, and dated ππ
π¦ where this page comes from
- alpine 3.23: tk9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tk9-doc 9.0.3-r2 Β· TCL
- fedora 43: tk-devel 1:9.0.2-1.fc43 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 43: tk-devel 1:9.0.2-1.fc43 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 44: tk-devel 1:9.0.2-1.fc44 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 44: tk-devel 1:9.0.2-1.fc44 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- nixos 26.05: tk 9.0.3
- nixpkgs unstable: tk 9.1.0
- Copyright (c) 1990 The Regents of the University of California
- Copyright (c) 1994-1998 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms