Tk_AllocCursorFromObj(3tk)

πŸ“¦
version 9.0.4 Β· 3 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ NAME ✨

Tk_AllocCursorFromObj, Tk_GetCursor, Tk_GetCursorFromObj, Tk_GetCursorFromData, Tk_NameOfCursor, Tk_FreeCursorFromObj, Tk_FreeCursor β€” bestie, we're maintaining a whole database of cursors πŸ’… (this is Tk Library Procedures, so get comfy)


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <tk.h>

Tk_Cursor
Tk_AllocCursorFromObj(interp, tkwin, objPtr)

Tk_Cursor
Tk_GetCursor(interp, tkwin, name)

Tk_Cursor
Tk_GetCursorFromObj(tkwin, objPtr)

Tk_Cursor
Tk_GetCursorFromData(interp, tkwin, source, mask, width, height, xHot, yHot, fg, bg)

const char *
Tk_NameOfCursor(display, cursor)

Tk_FreeCursorFromObj(tkwin, objPtr)

Tk_FreeCursor(display, cursor)

πŸŽ€ ARGUMENTS πŸŽ€

  • Tcl_Interp *interp (in) β€” the interpreter we send error reporting through, like a little messenger 🫢
  • Tk_Window tkwin (in) β€” the token for the window where this cursor is gonna live.
  • Tcl_Obj *objPtr (in/out) β€” the description of the cursor (values listed down below πŸ‘‡). Heads up: its internal rep gets modified so it can cache the pointer to the matching Tk_Cursor. It's giving "I remember you" energy.
  • const char *name (in) β€” same as objPtr except the cursor description comes in as a string and the resulting Tk_Cursor is not cached. no memory, just vibes.
  • const char *source (in) β€” the data for the cursor, in standard cursor format.
  • const char *mask (in) β€” the data for the mask cursor, in standard cursor format.
  • int width (in) β€” width of source and mask.
  • int height (in) β€” height of source and mask.
  • int xHot (in) β€” X-location of the cursor hot-spot ✨
  • int yHot (in) β€” Y-location of the cursor hot-spot ✨
  • const char *fg (in) β€” textual description of the foreground color for the cursor.
  • const char *bg (in) β€” textual description of the background color for the cursor.
  • Display *display (in) β€” the display the cursor was allocated for.
  • Tk_Cursor cursor (in) β€” opaque Tk identifier for the cursor. If you're passing it to Tk_FreeCursor, it must have come back from some earlier call to Tk_GetCursor or Tk_GetCursorFromData. No randos allowed πŸ’€

🌸 DESCRIPTION 🌸

Okay so these procedures manage a whole collection of cursors that an application is using. They let cursors get re-used efficiently (bye bye server overhead, we love to see it) and they also let cursors be named with character strings. iconic.

Tk_AllocCursorFromObj takes an object describing a cursor as its argument and returns an opaque Tk identifier for a cursor matching that description. If it can re-use an existing cursor it absolutely will, and otherwise it makes a new one. Tk_AllocCursorFromObj caches info about the return value in objPtr, which speeds up future calls to procedures like Tk_AllocCursorFromObj and Tk_GetCursorFromObj. If something goes wrong while creating the cursor β€” like when objPtr points at a file that does not exist 😭 β€” then None is returned and an error message gets stored in interp's result, as long as interp isn't NULL. ObjPtr must contain a standard Tcl list in one of these forms:

  • name [fgColor [bgColor]] β€” Name is the name of a cursor in the standard X cursor cursor, i.e., any of the names defined in cursorcursor.h, without the XC_. Some example values are X_cursor, hand2, or left_ptr. Appendix B of "The X Window System" by Scheifler & Gettys has illustrations showing what each of these cursors looks like. If fgColor and bgColor are both specified, they give the foreground and background colors to use for the cursor (any of the forms acceptable to Tk_GetColor may be used). If only fgColor is specified, then there will be no background color: the background will be transparent ✨. If no colors are specified, then the cursor will use black for its foreground color and white for its background color.

The Macintosh version of Tk supports all of the X cursors and will also accept any of the standard Mac cursors including ibeam, crosshair, watch, plus, and arrow. On top of that, Tk will load Macintosh cursor resources of the types crsr (color) and CURS (black and white) by the name of the resource. The application and all its open dynamic library's resource files will be searched for the named cursor. And if there are conflicts, color cursors will always be loaded in preference to black and white cursors. periodt.

  • @sourceName maskName fgColor bgColor β€” in this form, sourceName and maskName are the names of files describing cursors for the cursor's source bits and mask. Each file must be in standard X11 cursor format. FgColor and bgColor indicate the colors to use for the cursor, in any of the forms acceptable to Tk_GetColor. Heads up bestie: this form of the command will not work on Macintosh or Windows computers 🚫
  • @sourceName fgColor β€” this one is similar to the form above, except that the source is used as mask too. That means the cursor's background is transparent. And same deal: this form will not work on Macintosh or Windows computers.
  • @sourceName β€” this form only works on Windows, and it will load a Windows system cursor (.ani or .cur) from the file specified in sourceName.

Tk_GetCursor is identical to Tk_AllocCursorFromObj except that the description of the cursor is given with a string instead of an object. That stops Tk_GetCursor from caching the return value, so Tk_GetCursor is less efficient than Tk_AllocCursorFromObj. lowkey sad but okay.

Tk_GetCursorFromObj returns the token for an existing cursor, given the window and the description used to create that cursor. Tk_GetCursorFromObj does not actually create the cursor; it must already have been created by a previous call to Tk_AllocCursorFromObj or Tk_GetCursor. The return value is cached in objPtr, which speeds up future calls to Tk_GetCursorFromObj with the same objPtr and tkwin.

Tk_GetCursorFromData lets cursors be created from in-memory descriptions of their source and mask cursors. source points to standard cursor data for the cursor's source bits, and mask points to standard cursor data describing which pixels of source are to be drawn and which are to be considered transparent. width and height give the dimensions of the cursor, xHot and yHot indicate the location of the cursor's hot-spot (the point that is reported when an event occurs), and fg and bg describe the cursor's foreground and background colors textually (any of the forms suitable for Tk_GetColor may be used). Typically, the arguments to Tk_GetCursorFromData are created by including a cursor file directly into the source code for a program, like this:

Tk_Cursor cursor;
#include "source.cursor"
#include "mask.cursor"
cursor = Tk_GetCursorFromData(interp, tkwin, source_bits,
    mask_bits, source_width, source_height, source_x_hot,
    source_y_hot, "red", "blue");

Under normal conditions Tk_GetCursorFromData will return an identifier for the requested cursor. If an error occurs in creating the cursor, then None is returned and an error message will be stored in interp's result.

Tk_AllocCursorFromObj, Tk_GetCursor, and Tk_GetCursorFromData all maintain a database of every cursor they've created πŸ‘‘. Whenever possible, a call to Tk_AllocCursorFromObj, Tk_GetCursor, or Tk_GetCursorFromData will return an existing cursor instead of creating a new one. This approach can substantially reduce server overhead, so the Tk procedures should generally be used in preference to Xlib procedures like XCreateFontCursor or XCreatePixmapCursor, which create a new cursor on every single call 😭. The Tk procedures are also more portable than the lower-level X procedures. slay.

The procedure Tk_NameOfCursor is roughly the inverse of Tk_GetCursor. If its cursor argument was created by Tk_GetCursor, then the return value is the name argument that was passed to Tk_GetCursor to create the cursor. If cursor was created by a call to Tk_GetCursorFromData, or by any other mechanism, then the return value is a hexadecimal string giving the X identifier for the cursor. Note that the string returned by Tk_NameOfCursor is only guaranteed to persist until the next call to Tk_NameOfCursor. Also, this call is not portable except for cursors returned by Tk_GetCursor. so like, screenshot it if you need it ig.

When a cursor returned by Tk_AllocCursorFromObj, Tk_GetCursor, or Tk_GetCursorFromData is no longer needed, Tk_FreeCursorFromObj or Tk_FreeCursor should be called to release it. For Tk_FreeCursorFromObj the cursor to release is specified with the same information used to create it; for Tk_FreeCursor the cursor to release is specified with its Tk_Cursor token. There should be exactly one call to Tk_FreeCursor for each call to Tk_AllocCursorFromObj, Tk_GetCursor, or Tk_GetCursorFromData. one in, one out, that's the rule 🫢


πŸ’€ BUGS πŸ’€

When figuring out whether an existing cursor can satisfy a new request, Tk_AllocCursorFromObj, Tk_GetCursor, and Tk_GetCursorFromData consider only the immediate values of their arguments. For example, when a file name is passed to Tk_GetCursor, Tk_GetCursor will assume it is safe to re-use an existing cursor created from the same file name: it will not check to see whether the file itself has changed, or whether the current directory has changed, thereby causing the name to refer to a different file. Similarly, Tk_GetCursorFromData assumes that if the same source pointer is used in two different calls, then the pointers refer to the same data; it does not check to see if the actual data values have changed. it's trusting you, bestie, so don't play it 😭


🌈 KEYWORDS 🌈

cursor


and that's the whole page, my love β€” this is Tk 8.1, from Tk_AllocCursorFromObj(3tk), so go forth and re-use those cursors efficiently πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from