| 1 | /* |
| 2 | * tkCursor.c -- |
| 3 | * |
| 4 | * This file maintains a database of read-only cursors for the Tk |
| 5 | * toolkit. This allows cursors to be shared between widgets and |
| 6 | * also avoids round-trips to the X server. |
| 7 | * |
| 8 | * Copyright (c) 1990-1994 The Regents of the University of California. |
| 9 | * Copyright (c) 1994-1997 Sun Microsystems, Inc. |
| 10 | * |
| 11 | * See the file "license.terms" for information on usage and redistribution |
| 12 | * of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 13 | * |
| 14 | * RCS: @(#) $Id: tkCursor.c,v 1.9.2.1 2003/04/18 21:56:59 hobbs Exp $ |
| 15 | */ |
| 16 | |
| 17 | #include "tkPort.h" |
| 18 | #include "tkInt.h" |
| 19 | |
| 20 | /* |
| 21 | * A TkCursor structure exists for each cursor that is currently |
| 22 | * active. Each structure is indexed with two hash tables defined |
| 23 | * below. One of the tables is cursorIdTable, and the other is either |
| 24 | * cursorNameTable or cursorDataTable, each of which are stored in the |
| 25 | * TkDisplay structure for the current thread. |
| 26 | */ |
| 27 | |
| 28 | typedef struct { |
| 29 | CONST char *source; /* Cursor bits. */ |
| 30 | CONST char *mask; /* Mask bits. */ |
| 31 | int width, height; /* Dimensions of cursor (and data |
| 32 | * and mask). */ |
| 33 | int xHot, yHot; /* Location of cursor hot-spot. */ |
| 34 | Tk_Uid fg, bg; /* Colors for cursor. */ |
| 35 | Display *display; /* Display on which cursor will be used. */ |
| 36 | } DataKey; |
| 37 | |
| 38 | /* |
| 39 | * Forward declarations for procedures defined in this file: |
| 40 | */ |
| 41 | |
| 42 | static void CursorInit _ANSI_ARGS_((TkDisplay *dispPtr)); |
| 43 | static void DupCursorObjProc _ANSI_ARGS_((Tcl_Obj *srcObjPtr, |
| 44 | Tcl_Obj *dupObjPtr)); |
| 45 | static void FreeCursor _ANSI_ARGS_((TkCursor *cursorPtr)); |
| 46 | static void FreeCursorObjProc _ANSI_ARGS_((Tcl_Obj *objPtr)); |
| 47 | static TkCursor * TkcGetCursor _ANSI_ARGS_((Tcl_Interp *interp, |
| 48 | Tk_Window tkwin, CONST char *name)); |
| 49 | static TkCursor * GetCursorFromObj _ANSI_ARGS_((Tk_Window tkwin, |
| 50 | Tcl_Obj *objPtr)); |
| 51 | static void InitCursorObj _ANSI_ARGS_((Tcl_Obj *objPtr)); |
| 52 | |
| 53 | /* |
| 54 | * The following structure defines the implementation of the "cursor" Tcl |
| 55 | * object, used for drawing. The color object remembers the hash table |
| 56 | * entry associated with a color. The actual allocation and deallocation |
| 57 | * of the color should be done by the configuration package when the cursor |
| 58 | * option is set. |
| 59 | */ |
| 60 | |
| 61 | Tcl_ObjType tkCursorObjType = { |
| 62 | "cursor", /* name */ |
| 63 | FreeCursorObjProc, /* freeIntRepProc */ |
| 64 | DupCursorObjProc, /* dupIntRepProc */ |
| 65 | NULL, /* updateStringProc */ |
| 66 | NULL /* setFromAnyProc */ |
| 67 | }; |
| 68 | |
| 69 | /* |
| 70 | *---------------------------------------------------------------------- |
| 71 | * |
| 72 | * Tk_AllocCursorFromObj -- |
| 73 | * |
| 74 | * Given a Tcl_Obj *, map the value to a corresponding |
| 75 | * Tk_Cursor structure based on the tkwin given. |
| 76 | * |
| 77 | * Results: |
| 78 | * The return value is the X identifer for the desired cursor, |
| 79 | * unless objPtr couldn't be parsed correctly. In this case, |
| 80 | * None is returned and an error message is left in the interp's result. |
| 81 | * The caller should never modify the cursor that is returned, and |
| 82 | * should eventually call Tk_FreeCursorFromObj when the cursor is no |
| 83 | * longer needed. |
| 84 | * |
| 85 | * Side effects: |
| 86 | * The cursor is added to an internal database with a reference count. |
| 87 | * For each call to this procedure, there should eventually be a call |
| 88 | * to Tk_FreeCursorFromObj, so that the database can be cleaned up |
| 89 | * when cursors aren't needed anymore. |
| 90 | * |
| 91 | *---------------------------------------------------------------------- |
| 92 | */ |
| 93 | |
| 94 | Tk_Cursor |
| 95 | Tk_AllocCursorFromObj(interp, tkwin, objPtr) |
| 96 | Tcl_Interp *interp; /* Interp for error results. */ |
| 97 | Tk_Window tkwin; /* Window in which the cursor will be used.*/ |
| 98 | Tcl_Obj *objPtr; /* Object describing cursor; see manual |
| 99 | * entry for description of legal |
| 100 | * syntax of this obj's string rep. */ |
| 101 | { |
| 102 | TkCursor *cursorPtr; |
| 103 | |
| 104 | if (TclObjGetType(objPtr) != &tkCursorObjType) { |
| 105 | InitCursorObj(objPtr); |
| 106 | } |
| 107 | cursorPtr = (TkCursor *) TclObjInternal(objPtr)->twoPtrValue.ptr1; |
| 108 | |
| 109 | /* |
| 110 | * If the object currently points to a TkCursor, see if it's the |
| 111 | * one we want. If so, increment its reference count and return. |
| 112 | */ |
| 113 | |
| 114 | if (cursorPtr != NULL) { |
| 115 | if (cursorPtr->resourceRefCount == 0) { |
| 116 | /* |
| 117 | * This is a stale reference: it refers to a TkCursor that's |
| 118 | * no longer in use. Clear the reference. |
| 119 | */ |
| 120 | FreeCursorObjProc(objPtr); |
| 121 | cursorPtr = NULL; |
| 122 | } else if (Tk_Display(tkwin) == cursorPtr->display) { |
| 123 | cursorPtr->resourceRefCount++; |
| 124 | return cursorPtr->cursor; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * The object didn't point to the TkCursor that we wanted. Search |
| 130 | * the list of TkCursors with the same name to see if one of the |
| 131 | * other TkCursors is the right one. |
| 132 | */ |
| 133 | |
| 134 | if (cursorPtr != NULL) { |
| 135 | TkCursor *firstCursorPtr = |
| 136 | (TkCursor *) Tcl_GetHashValue(cursorPtr->hashPtr); |
| 137 | FreeCursorObjProc(objPtr); |
| 138 | for (cursorPtr = firstCursorPtr; cursorPtr != NULL; |
| 139 | cursorPtr = cursorPtr->nextPtr) { |
| 140 | if (Tk_Display(tkwin) == cursorPtr->display) { |
| 141 | cursorPtr->resourceRefCount++; |
| 142 | cursorPtr->objRefCount++; |
| 143 | TclObjInternal(objPtr)->twoPtrValue.ptr1 = (VOID *) cursorPtr; |
| 144 | return cursorPtr->cursor; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Still no luck. Call TkcGetCursor to allocate a new TkCursor object. |
| 151 | */ |
| 152 | |
| 153 | cursorPtr = TkcGetCursor(interp, tkwin, Tcl_GetString(objPtr)); |
| 154 | objPtr->internalRep.twoPtrValue.ptr1 = (VOID *) cursorPtr; |
| 155 | if (cursorPtr == NULL) { |
| 156 | return None; |
| 157 | } else { |
| 158 | cursorPtr->objRefCount++; |
| 159 | return cursorPtr->cursor; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | *---------------------------------------------------------------------- |
| 165 | * |
| 166 | * Tk_GetCursor -- |
| 167 | * |
| 168 | * Given a string describing a cursor, locate (or create if necessary) |
| 169 | * a cursor that fits the description. |
| 170 | * |
| 171 | * Results: |
| 172 | * The return value is the X identifer for the desired cursor, |
| 173 | * unless string couldn't be parsed correctly. In this case, |
| 174 | * None is returned and an error message is left in the interp's result. |
| 175 | * The caller should never modify the cursor that is returned, and |
| 176 | * should eventually call Tk_FreeCursor when the cursor is no longer |
| 177 | * needed. |
| 178 | * |
| 179 | * Side effects: |
| 180 | * The cursor is added to an internal database with a reference count. |
| 181 | * For each call to this procedure, there should eventually be a call |
| 182 | * to Tk_FreeCursor, so that the database can be cleaned up when cursors |
| 183 | * aren't needed anymore. |
| 184 | * |
| 185 | *---------------------------------------------------------------------- |
| 186 | */ |
| 187 | |
| 188 | Tk_Cursor |
| 189 | Tk_GetCursor(interp, tkwin, string) |
| 190 | Tcl_Interp *interp; /* Interpreter to use for error reporting. */ |
| 191 | Tk_Window tkwin; /* Window in which cursor will be used. */ |
| 192 | Tk_Uid string; /* Description of cursor. See manual entry |
| 193 | * for details on legal syntax. */ |
| 194 | { |
| 195 | TkCursor *cursorPtr = TkcGetCursor(interp, tkwin, string); |
| 196 | if (cursorPtr == NULL) { |
| 197 | return None; |
| 198 | } |
| 199 | return cursorPtr->cursor; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | *---------------------------------------------------------------------- |
| 204 | * |
| 205 | * TkcGetCursor -- |
| 206 | * |
| 207 | * Given a string describing a cursor, locate (or create if necessary) |
| 208 | * a cursor that fits the description. This routine returns the |
| 209 | * internal data structure for the cursor, which avoids extra |
| 210 | * hash table lookups in Tk_AllocCursorFromObj. |
| 211 | * |
| 212 | * Results: |
| 213 | * The return value is a pointer to the TkCursor for the desired |
| 214 | * cursor, unless string couldn't be parsed correctly. In this |
| 215 | * case, NULL is returned and an error message is left in the |
| 216 | * interp's result. The caller should never modify the cursor that |
| 217 | * is returned, and should eventually call Tk_FreeCursor when the |
| 218 | * cursor is no longer needed. |
| 219 | * |
| 220 | * Side effects: |
| 221 | * The cursor is added to an internal database with a reference count. |
| 222 | * For each call to this procedure, there should eventually be a call |
| 223 | * to Tk_FreeCursor, so that the database can be cleaned up when cursors |
| 224 | * aren't needed anymore. |
| 225 | * |
| 226 | *---------------------------------------------------------------------- |
| 227 | */ |
| 228 | |
| 229 | static TkCursor * |
| 230 | TkcGetCursor(interp, tkwin, string) |
| 231 | Tcl_Interp *interp; /* Interpreter to use for error reporting. */ |
| 232 | Tk_Window tkwin; /* Window in which cursor will be used. */ |
| 233 | CONST char *string; /* Description of cursor. See manual entry |
| 234 | * for details on legal syntax. */ |
| 235 | { |
| 236 | Tcl_HashEntry *nameHashPtr; |
| 237 | register TkCursor *cursorPtr; |
| 238 | TkCursor *existingCursorPtr = NULL; |
| 239 | int new; |
| 240 | TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr; |
| 241 | |
| 242 | if (!dispPtr->cursorInit) { |
| 243 | CursorInit(dispPtr); |
| 244 | } |
| 245 | |
| 246 | nameHashPtr = Tcl_CreateHashEntry(&dispPtr->cursorNameTable, |
| 247 | string, &new); |
| 248 | if (!new) { |
| 249 | existingCursorPtr = (TkCursor *) Tcl_GetHashValue(nameHashPtr); |
| 250 | for (cursorPtr = existingCursorPtr; cursorPtr != NULL; |
| 251 | cursorPtr = cursorPtr->nextPtr) { |
| 252 | if (Tk_Display(tkwin) == cursorPtr->display) { |
| 253 | cursorPtr->resourceRefCount++; |
| 254 | return cursorPtr; |
| 255 | } |
| 256 | } |
| 257 | } else { |
| 258 | existingCursorPtr = NULL; |
| 259 | } |
| 260 | |
| 261 | cursorPtr = TkGetCursorByName(interp, tkwin, string); |
| 262 | |
| 263 | if (cursorPtr == NULL) { |
| 264 | if (new) { |
| 265 | Tcl_DeleteHashEntry(nameHashPtr); |
| 266 | } |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Add information about this cursor to our database. |
| 272 | */ |
| 273 | |
| 274 | cursorPtr->display = Tk_Display(tkwin); |
| 275 | cursorPtr->resourceRefCount = 1; |
| 276 | cursorPtr->objRefCount = 0; |
| 277 | cursorPtr->otherTable = &dispPtr->cursorNameTable; |
| 278 | cursorPtr->hashPtr = nameHashPtr; |
| 279 | cursorPtr->nextPtr = existingCursorPtr; |
| 280 | cursorPtr->idHashPtr = Tcl_CreateHashEntry(&dispPtr->cursorIdTable, |
| 281 | (char *) cursorPtr->cursor, &new); |
| 282 | if (!new) { |
| 283 | panic("cursor already registered in Tk_GetCursor"); |
| 284 | } |
| 285 | Tcl_SetHashValue(nameHashPtr, cursorPtr); |
| 286 | Tcl_SetHashValue(cursorPtr->idHashPtr, cursorPtr); |
| 287 | |
| 288 | return cursorPtr; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | *---------------------------------------------------------------------- |
| 293 | * |
| 294 | * Tk_GetCursorFromData -- |
| 295 | * |
| 296 | * Given a description of the bits and colors for a cursor, |
| 297 | * make a cursor that has the given properties. |
| 298 | * |
| 299 | * Results: |
| 300 | * The return value is the X identifer for the desired cursor, |
| 301 | * unless it couldn't be created properly. In this case, None is |
| 302 | * returned and an error message is left in the interp's result. The |
| 303 | * caller should never modify the cursor that is returned, and |
| 304 | * should eventually call Tk_FreeCursor when the cursor is no |
| 305 | * longer needed. |
| 306 | * |
| 307 | * Side effects: |
| 308 | * The cursor is added to an internal database with a reference count. |
| 309 | * For each call to this procedure, there should eventually be a call |
| 310 | * to Tk_FreeCursor, so that the database can be cleaned up when cursors |
| 311 | * aren't needed anymore. |
| 312 | * |
| 313 | *---------------------------------------------------------------------- |
| 314 | */ |
| 315 | |
| 316 | Tk_Cursor |
| 317 | Tk_GetCursorFromData(interp, tkwin, source, mask, width, height, |
| 318 | xHot, yHot, fg, bg) |
| 319 | Tcl_Interp *interp; /* Interpreter to use for error reporting. */ |
| 320 | Tk_Window tkwin; /* Window in which cursor will be used. */ |
| 321 | CONST char *source; /* Bitmap data for cursor shape. */ |
| 322 | CONST char *mask; /* Bitmap data for cursor mask. */ |
| 323 | int width, height; /* Dimensions of cursor. */ |
| 324 | int xHot, yHot; /* Location of hot-spot in cursor. */ |
| 325 | Tk_Uid fg; /* Foreground color for cursor. */ |
| 326 | Tk_Uid bg; /* Background color for cursor. */ |
| 327 | { |
| 328 | DataKey dataKey; |
| 329 | Tcl_HashEntry *dataHashPtr; |
| 330 | register TkCursor *cursorPtr; |
| 331 | int new; |
| 332 | XColor fgColor, bgColor; |
| 333 | TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr; |
| 334 | |
| 335 | |
| 336 | if (!dispPtr->cursorInit) { |
| 337 | CursorInit(dispPtr); |
| 338 | } |
| 339 | |
| 340 | dataKey.source = source; |
| 341 | dataKey.mask = mask; |
| 342 | dataKey.width = width; |
| 343 | dataKey.height = height; |
| 344 | dataKey.xHot = xHot; |
| 345 | dataKey.yHot = yHot; |
| 346 | dataKey.fg = fg; |
| 347 | dataKey.bg = bg; |
| 348 | dataKey.display = Tk_Display(tkwin); |
| 349 | dataHashPtr = Tcl_CreateHashEntry(&dispPtr->cursorDataTable, |
| 350 | (char *) &dataKey, &new); |
| 351 | if (!new) { |
| 352 | cursorPtr = (TkCursor *) Tcl_GetHashValue(dataHashPtr); |
| 353 | cursorPtr->resourceRefCount++; |
| 354 | return cursorPtr->cursor; |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * No suitable cursor exists yet. Make one using the data |
| 359 | * available and add it to the database. |
| 360 | */ |
| 361 | |
| 362 | if (XParseColor(dataKey.display, Tk_Colormap(tkwin), fg, &fgColor) == 0) { |
| 363 | Tcl_AppendResult(interp, "invalid color name \"", fg, "\"", |
| 364 | (char *) NULL); |
| 365 | goto error; |
| 366 | } |
| 367 | if (XParseColor(dataKey.display, Tk_Colormap(tkwin), bg, &bgColor) == 0) { |
| 368 | Tcl_AppendResult(interp, "invalid color name \"", bg, "\"", |
| 369 | (char *) NULL); |
| 370 | goto error; |
| 371 | } |
| 372 | |
| 373 | cursorPtr = TkCreateCursorFromData(tkwin, source, mask, width, height, |
| 374 | xHot, yHot, fgColor, bgColor); |
| 375 | |
| 376 | if (cursorPtr == NULL) { |
| 377 | goto error; |
| 378 | } |
| 379 | |
| 380 | cursorPtr->resourceRefCount = 1; |
| 381 | cursorPtr->otherTable = &dispPtr->cursorDataTable; |
| 382 | cursorPtr->hashPtr = dataHashPtr; |
| 383 | cursorPtr->objRefCount = 0; |
| 384 | cursorPtr->idHashPtr = Tcl_CreateHashEntry(&dispPtr->cursorIdTable, |
| 385 | (char *) cursorPtr->cursor, &new); |
| 386 | cursorPtr->nextPtr = NULL; |
| 387 | |
| 388 | if (!new) { |
| 389 | panic("cursor already registered in Tk_GetCursorFromData"); |
| 390 | } |
| 391 | Tcl_SetHashValue(dataHashPtr, cursorPtr); |
| 392 | Tcl_SetHashValue(cursorPtr->idHashPtr, cursorPtr); |
| 393 | return cursorPtr->cursor; |
| 394 | |
| 395 | error: |
| 396 | Tcl_DeleteHashEntry(dataHashPtr); |
| 397 | return None; |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | *-------------------------------------------------------------- |
| 402 | * |
| 403 | * Tk_NameOfCursor -- |
| 404 | * |
| 405 | * Given a cursor, return a textual string identifying it. |
| 406 | * |
| 407 | * Results: |
| 408 | * If cursor was created by Tk_GetCursor, then the return |
| 409 | * value is the "string" that was used to create it. |
| 410 | * Otherwise the return value is a string giving the X |
| 411 | * identifier for the cursor. The storage for the returned |
| 412 | * string is only guaranteed to persist up until the next |
| 413 | * call to this procedure. |
| 414 | * |
| 415 | * Side effects: |
| 416 | * None. |
| 417 | * |
| 418 | *-------------------------------------------------------------- |
| 419 | */ |
| 420 | |
| 421 | CONST char * |
| 422 | Tk_NameOfCursor(display, cursor) |
| 423 | Display *display; /* Display for which cursor was allocated. */ |
| 424 | Tk_Cursor cursor; /* Identifier for cursor whose name is |
| 425 | * wanted. */ |
| 426 | { |
| 427 | Tcl_HashEntry *idHashPtr; |
| 428 | TkCursor *cursorPtr; |
| 429 | TkDisplay *dispPtr; |
| 430 | |
| 431 | dispPtr = TkGetDisplay(display); |
| 432 | |
| 433 | if (!dispPtr->cursorInit) { |
| 434 | printid: |
| 435 | sprintf(dispPtr->cursorString, "cursor id 0x%x", |
| 436 | (unsigned int) cursor); |
| 437 | return dispPtr->cursorString; |
| 438 | } |
| 439 | idHashPtr = Tcl_FindHashEntry(&dispPtr->cursorIdTable, (char *) cursor); |
| 440 | if (idHashPtr == NULL) { |
| 441 | goto printid; |
| 442 | } |
| 443 | cursorPtr = (TkCursor *) Tcl_GetHashValue(idHashPtr); |
| 444 | if (cursorPtr->otherTable != &dispPtr->cursorNameTable) { |
| 445 | goto printid; |
| 446 | } |
| 447 | return cursorPtr->hashPtr->key.string; |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | *---------------------------------------------------------------------- |
| 452 | * |
| 453 | * FreeCursor -- |
| 454 | * |
| 455 | * This procedure is invoked by both Tk_FreeCursor and |
| 456 | * Tk_FreeCursorFromObj; it does all the real work of deallocating |
| 457 | * a cursor. |
| 458 | * |
| 459 | * Results: |
| 460 | * None. |
| 461 | * |
| 462 | * Side effects: |
| 463 | * The reference count associated with cursor is decremented, and |
| 464 | * it is officially deallocated if no-one is using it anymore. |
| 465 | * |
| 466 | *---------------------------------------------------------------------- |
| 467 | */ |
| 468 | |
| 469 | static void |
| 470 | FreeCursor(cursorPtr) |
| 471 | TkCursor *cursorPtr; /* Cursor to be released. */ |
| 472 | { |
| 473 | TkCursor *prevPtr; |
| 474 | |
| 475 | cursorPtr->resourceRefCount--; |
| 476 | if (cursorPtr->resourceRefCount > 0) { |
| 477 | return; |
| 478 | } |
| 479 | |
| 480 | Tcl_DeleteHashEntry(cursorPtr->idHashPtr); |
| 481 | prevPtr = (TkCursor *) Tcl_GetHashValue(cursorPtr->hashPtr); |
| 482 | if (prevPtr == cursorPtr) { |
| 483 | if (cursorPtr->nextPtr == NULL) { |
| 484 | Tcl_DeleteHashEntry(cursorPtr->hashPtr); |
| 485 | } else { |
| 486 | Tcl_SetHashValue(cursorPtr->hashPtr, cursorPtr->nextPtr); |
| 487 | } |
| 488 | } else { |
| 489 | while (prevPtr->nextPtr != cursorPtr) { |
| 490 | prevPtr = prevPtr->nextPtr; |
| 491 | } |
| 492 | prevPtr->nextPtr = cursorPtr->nextPtr; |
| 493 | } |
| 494 | TkpFreeCursor(cursorPtr); |
| 495 | if (cursorPtr->objRefCount == 0) { |
| 496 | ckfree((char *) cursorPtr); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | *---------------------------------------------------------------------- |
| 502 | * |
| 503 | * Tk_FreeCursor -- |
| 504 | * |
| 505 | * This procedure is called to release a cursor allocated by |
| 506 | * Tk_GetCursor or TkGetCursorFromData. |
| 507 | * |
| 508 | * Results: |
| 509 | * None. |
| 510 | * |
| 511 | * Side effects: |
| 512 | * The reference count associated with cursor is decremented, and |
| 513 | * it is officially deallocated if no-one is using it anymore. |
| 514 | * |
| 515 | *---------------------------------------------------------------------- |
| 516 | */ |
| 517 | |
| 518 | void |
| 519 | Tk_FreeCursor(display, cursor) |
| 520 | Display *display; /* Display for which cursor was allocated. */ |
| 521 | Tk_Cursor cursor; /* Identifier for cursor to be released. */ |
| 522 | { |
| 523 | Tcl_HashEntry *idHashPtr; |
| 524 | TkDisplay *dispPtr = TkGetDisplay(display); |
| 525 | |
| 526 | if (!dispPtr->cursorInit) { |
| 527 | panic("Tk_FreeCursor called before Tk_GetCursor"); |
| 528 | } |
| 529 | |
| 530 | idHashPtr = Tcl_FindHashEntry(&dispPtr->cursorIdTable, (char *) cursor); |
| 531 | if (idHashPtr == NULL) { |
| 532 | panic("Tk_FreeCursor received unknown cursor argument"); |
| 533 | } |
| 534 | FreeCursor((TkCursor *) Tcl_GetHashValue(idHashPtr)); |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | *---------------------------------------------------------------------- |
| 539 | * |
| 540 | * Tk_FreeCursorFromObj -- |
| 541 | * |
| 542 | * This procedure is called to release a cursor allocated by |
| 543 | * Tk_AllocCursorFromObj. It does not throw away the Tcl_Obj *; |
| 544 | * it only gets rid of the hash table entry for this cursor |
| 545 | * and clears the cached value that is normally stored in the object. |
| 546 | * |
| 547 | * Results: |
| 548 | * None. |
| 549 | * |
| 550 | * Side effects: |
| 551 | * The reference count associated with the cursor represented by |
| 552 | * objPtr is decremented, and the cursor is released to X if there are |
| 553 | * no remaining uses for it. |
| 554 | * |
| 555 | *---------------------------------------------------------------------- |
| 556 | */ |
| 557 | |
| 558 | void |
| 559 | Tk_FreeCursorFromObj(tkwin, objPtr) |
| 560 | Tk_Window tkwin; /* The window this cursor lives in. Needed |
| 561 | * for the display value. */ |
| 562 | Tcl_Obj *objPtr; /* The Tcl_Obj * to be freed. */ |
| 563 | { |
| 564 | FreeCursor(GetCursorFromObj(tkwin, objPtr)); |
| 565 | FreeCursorObjProc(objPtr); |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | *--------------------------------------------------------------------------- |
| 570 | * |
| 571 | * FreeCursorFromObjProc -- |
| 572 | * |
| 573 | * This proc is called to release an object reference to a cursor. |
| 574 | * Called when the object's internal rep is released or when |
| 575 | * the cached tkColPtr needs to be changed. |
| 576 | * |
| 577 | * Results: |
| 578 | * None. |
| 579 | * |
| 580 | * Side effects: |
| 581 | * The object reference count is decremented. When both it |
| 582 | * and the hash ref count go to zero, the color's resources |
| 583 | * are released. |
| 584 | * |
| 585 | *--------------------------------------------------------------------------- |
| 586 | */ |
| 587 | |
| 588 | static void |
| 589 | FreeCursorObjProc(objPtr) |
| 590 | Tcl_Obj *objPtr; /* The object we are releasing. */ |
| 591 | { |
| 592 | TkCursor *cursorPtr = (TkCursor *) objPtr->internalRep.twoPtrValue.ptr1; |
| 593 | |
| 594 | if (cursorPtr != NULL) { |
| 595 | cursorPtr->objRefCount--; |
| 596 | if ((cursorPtr->objRefCount == 0) |
| 597 | && (cursorPtr->resourceRefCount == 0)) { |
| 598 | ckfree((char *) cursorPtr); |
| 599 | } |
| 600 | objPtr->internalRep.twoPtrValue.ptr1 = (VOID *) NULL; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | *--------------------------------------------------------------------------- |
| 606 | * |
| 607 | * DupCursorObjProc -- |
| 608 | * |
| 609 | * When a cached cursor object is duplicated, this is called to |
| 610 | * update the internal reps. |
| 611 | * |
| 612 | * Results: |
| 613 | * None. |
| 614 | * |
| 615 | * Side effects: |
| 616 | * The color's objRefCount is incremented and the internal rep |
| 617 | * of the copy is set to point to it. |
| 618 | * |
| 619 | *--------------------------------------------------------------------------- |
| 620 | */ |
| 621 | |
| 622 | static void |
| 623 | DupCursorObjProc(srcObjPtr, dupObjPtr) |
| 624 | Tcl_Obj *srcObjPtr; /* The object we are copying from. */ |
| 625 | Tcl_Obj *dupObjPtr; /* The object we are copying to. */ |
| 626 | { |
| 627 | TkCursor *cursorPtr = (TkCursor *) srcObjPtr->internalRep.twoPtrValue.ptr1; |
| 628 | |
| 629 | dupObjPtr->typePtr = srcObjPtr->typePtr; |
| 630 | dupObjPtr->internalRep.twoPtrValue.ptr1 = (VOID *) cursorPtr; |
| 631 | |
| 632 | if (cursorPtr != NULL) { |
| 633 | cursorPtr->objRefCount++; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | *---------------------------------------------------------------------- |
| 639 | * |
| 640 | * Tk_GetCursorFromObj -- |
| 641 | * |
| 642 | * Returns the cursor referred to buy a Tcl object. The cursor must |
| 643 | * already have been allocated via a call to Tk_AllocCursorFromObj or |
| 644 | * Tk_GetCursor. |
| 645 | * |
| 646 | * Results: |
| 647 | * Returns the Tk_Cursor that matches the tkwin and the string rep |
| 648 | * of the name of the cursor given in objPtr. |
| 649 | * |
| 650 | * Side effects: |
| 651 | * If the object is not already a cursor, the conversion will free |
| 652 | * any old internal representation. |
| 653 | * |
| 654 | *---------------------------------------------------------------------- |
| 655 | */ |
| 656 | |
| 657 | Tk_Cursor |
| 658 | Tk_GetCursorFromObj(tkwin, objPtr) |
| 659 | Tk_Window tkwin; |
| 660 | Tcl_Obj *objPtr; /* The object from which to get pixels. */ |
| 661 | { |
| 662 | TkCursor *cursorPtr = GetCursorFromObj(tkwin, objPtr); |
| 663 | /* GetCursorFromObj should never return NULL */ |
| 664 | return cursorPtr->cursor; |
| 665 | } |
| 666 | |
| 667 | /* |
| 668 | *---------------------------------------------------------------------- |
| 669 | * |
| 670 | * GetCursorFromObj -- |
| 671 | * |
| 672 | * Returns the cursor referred to by a Tcl object. The cursor must |
| 673 | * already have been allocated via a call to Tk_AllocCursorFromObj |
| 674 | * or Tk_GetCursor. |
| 675 | * |
| 676 | * Results: |
| 677 | * Returns the TkCursor * that matches the tkwin and the string rep |
| 678 | * of the name of the cursor given in objPtr. |
| 679 | * |
| 680 | * Side effects: |
| 681 | * If the object is not already a cursor, the conversion will free |
| 682 | * any old internal representation. |
| 683 | * |
| 684 | *---------------------------------------------------------------------- |
| 685 | */ |
| 686 | |
| 687 | static TkCursor * |
| 688 | GetCursorFromObj(tkwin, objPtr) |
| 689 | Tk_Window tkwin; /* Window in which the cursor will be used. */ |
| 690 | Tcl_Obj *objPtr; /* The object that describes the desired |
| 691 | * cursor. */ |
| 692 | { |
| 693 | TkCursor *cursorPtr; |
| 694 | Tcl_HashEntry *hashPtr; |
| 695 | TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr; |
| 696 | |
| 697 | if (TclObjGetType(objPtr) != &tkCursorObjType) { |
| 698 | InitCursorObj(objPtr); |
| 699 | } |
| 700 | |
| 701 | /* |
| 702 | * The internal representation is a cache of the last cursor used |
| 703 | * with the given name. But there can be lots different cursors |
| 704 | * for each cursor name; one cursor for each display. Check to |
| 705 | * see if the cursor we have cached is the one that is needed. |
| 706 | */ |
| 707 | cursorPtr = (TkCursor *) objPtr->internalRep.twoPtrValue.ptr1; |
| 708 | if ((cursorPtr != NULL) && (Tk_Display(tkwin) == cursorPtr->display)) { |
| 709 | return cursorPtr; |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * If we get to here, it means the cursor we need is not in the cache. |
| 714 | * Try to look up the cursor in the TkDisplay structure of the window. |
| 715 | */ |
| 716 | |
| 717 | hashPtr = Tcl_FindHashEntry(&dispPtr->cursorNameTable, |
| 718 | Tcl_GetString(objPtr)); |
| 719 | if (hashPtr == NULL) { |
| 720 | goto error; |
| 721 | } |
| 722 | for (cursorPtr = (TkCursor *) Tcl_GetHashValue(hashPtr); |
| 723 | cursorPtr != NULL; cursorPtr = cursorPtr->nextPtr) { |
| 724 | if (Tk_Display(tkwin) == cursorPtr->display) { |
| 725 | FreeCursorObjProc(objPtr); |
| 726 | objPtr->internalRep.twoPtrValue.ptr1 = (VOID *) cursorPtr; |
| 727 | cursorPtr->objRefCount++; |
| 728 | return cursorPtr; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | error: |
| 733 | panic("GetCursorFromObj called with non-existent cursor!"); |
| 734 | /* |
| 735 | * The following code isn't reached; it's just there to please compilers. |
| 736 | */ |
| 737 | return NULL; |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | *---------------------------------------------------------------------- |
| 742 | * |
| 743 | * InitCursorObj -- |
| 744 | * |
| 745 | * Bookeeping procedure to change an objPtr to a cursor type. |
| 746 | * |
| 747 | * Results: |
| 748 | * None. |
| 749 | * |
| 750 | * Side effects: |
| 751 | * The old internal rep of the object is freed. The internal |
| 752 | * rep is cleared. The final form of the object is set |
| 753 | * by either Tk_AllocCursorFromObj or GetCursorFromObj. |
| 754 | * |
| 755 | *---------------------------------------------------------------------- |
| 756 | */ |
| 757 | |
| 758 | static void |
| 759 | InitCursorObj(objPtr) |
| 760 | Tcl_Obj *objPtr; /* The object to convert. */ |
| 761 | { |
| 762 | Tcl_ObjType *typePtr; |
| 763 | |
| 764 | /* |
| 765 | * Free the old internalRep before setting the new one. |
| 766 | */ |
| 767 | |
| 768 | Tcl_GetString(objPtr); |
| 769 | typePtr = TclObjGetType(objPtr); |
| 770 | if ((typePtr != NULL) && (typePtr->freeIntRepProc != NULL)) { |
| 771 | (*typePtr->freeIntRepProc)(objPtr); |
| 772 | } |
| 773 | TclObjSetType(objPtr,&tkCursorObjType); |
| 774 | objPtr->internalRep.twoPtrValue.ptr1 = (VOID *) NULL; |
| 775 | } |
| 776 | |
| 777 | /* |
| 778 | *---------------------------------------------------------------------- |
| 779 | * |
| 780 | * CursorInit -- |
| 781 | * |
| 782 | * Initialize the structures used for cursor management. |
| 783 | * |
| 784 | * Results: |
| 785 | * None. |
| 786 | * |
| 787 | * Side effects: |
| 788 | * Read the code. |
| 789 | * |
| 790 | *---------------------------------------------------------------------- |
| 791 | */ |
| 792 | |
| 793 | static void |
| 794 | CursorInit(dispPtr) |
| 795 | TkDisplay *dispPtr; /* Display used to store thread-specific data. */ |
| 796 | { |
| 797 | Tcl_InitHashTable(&dispPtr->cursorNameTable, TCL_STRING_KEYS); |
| 798 | Tcl_InitHashTable(&dispPtr->cursorDataTable, sizeof(DataKey)/sizeof(int)); |
| 799 | |
| 800 | /* |
| 801 | * The call below is tricky: can't use sizeof(IdKey) because it |
| 802 | * gets padded with extra unpredictable bytes on some 64-bit |
| 803 | * machines. |
| 804 | */ |
| 805 | |
| 806 | /* |
| 807 | * Old code.... |
| 808 | * Tcl_InitHashTable(&dispPtr->cursorIdTable, sizeof(Display *) |
| 809 | * /sizeof(int)); |
| 810 | * |
| 811 | * The comment above doesn't make sense. |
| 812 | * However, XIDs should only be 32 bits, by the definition of X, |
| 813 | * so the code above causes Tk to crash. Here is the real code: |
| 814 | */ |
| 815 | |
| 816 | Tcl_InitHashTable(&dispPtr->cursorIdTable, TCL_ONE_WORD_KEYS); |
| 817 | |
| 818 | dispPtr->cursorInit = 1; |
| 819 | } |
| 820 | |
| 821 | /* |
| 822 | *---------------------------------------------------------------------- |
| 823 | * |
| 824 | * TkDebugCursor -- |
| 825 | * |
| 826 | * This procedure returns debugging information about a cursor. |
| 827 | * |
| 828 | * Results: |
| 829 | * The return value is a list with one sublist for each TkCursor |
| 830 | * corresponding to "name". Each sublist has two elements that |
| 831 | * contain the resourceRefCount and objRefCount fields from the |
| 832 | * TkCursor structure. |
| 833 | * |
| 834 | * Side effects: |
| 835 | * None. |
| 836 | * |
| 837 | *---------------------------------------------------------------------- |
| 838 | */ |
| 839 | |
| 840 | Tcl_Obj * |
| 841 | TkDebugCursor(tkwin, name) |
| 842 | Tk_Window tkwin; /* The window in which the cursor will be |
| 843 | * used (not currently used). */ |
| 844 | char *name; /* Name of the desired color. */ |
| 845 | { |
| 846 | TkCursor *cursorPtr; |
| 847 | Tcl_HashEntry *hashPtr; |
| 848 | Tcl_Obj *resultPtr, *objPtr; |
| 849 | TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr; |
| 850 | |
| 851 | resultPtr = Tcl_NewObj(); |
| 852 | hashPtr = Tcl_FindHashEntry(&dispPtr->cursorNameTable, name); |
| 853 | if (hashPtr != NULL) { |
| 854 | cursorPtr = (TkCursor *) Tcl_GetHashValue(hashPtr); |
| 855 | if (cursorPtr == NULL) { |
| 856 | panic("TkDebugCursor found empty hash table entry"); |
| 857 | } |
| 858 | for ( ; (cursorPtr != NULL); cursorPtr = cursorPtr->nextPtr) { |
| 859 | objPtr = Tcl_NewObj(); |
| 860 | Tcl_ListObjAppendElement(NULL, objPtr, |
| 861 | Tcl_NewIntObj(cursorPtr->resourceRefCount)); |
| 862 | Tcl_ListObjAppendElement(NULL, objPtr, |
| 863 | Tcl_NewIntObj(cursorPtr->objRefCount)); |
| 864 | Tcl_ListObjAppendElement(NULL, resultPtr, objPtr); |
| 865 | } |
| 866 | } |
| 867 | return resultPtr; |
| 868 | } |
| 869 |