]> cvs.zerfleddert.de Git - micropolis/blob - src/tk/tkcursor.c
Fixes for compilation with gcc 15
[micropolis] / src / tk / tkcursor.c
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 1990 Regents of the University of California
9 * Permission to use, copy, modify, and distribute this
10 * software and its documentation for any purpose and without
11 * fee is hereby granted, provided that the above copyright
12 * notice appear in all copies. The University of California
13 * makes no representations about the suitability of this
14 * software for any purpose. It is provided "as is" without
15 * express or implied warranty.
16 */
17
18 #ifndef lint
19 static char rcsid[] = "$Header: /user6/ouster/wish/RCS/tkCursor.c,v 1.12 91/10/31 11:40:41 ouster Exp $ SPRITE (Berkeley)";
20 #endif /* not lint */
21
22 #include "tkconfig.h"
23 #include "tkint.h"
24
25 /*
26 * One of the following data structures exists for each cursor that is
27 * currently active. Each structure is indexed with two hash tables
28 * defined below. One of the tables is idTable, and the other is either
29 * nameTable or dataTable, also defined below.
30 * .
31 */
32
33 typedef struct {
34 Cursor cursor; /* X identifier for cursor. */
35 Display *display; /* Display for which cursor is valid. */
36 int refCount; /* Number of active uses of cursor. */
37 Tcl_HashTable *otherTable; /* Second table (other than idTable) used
38 * to index this entry. */
39 Tcl_HashEntry *hashPtr; /* Entry in otherTable for this structure
40 * (needed when deleting). */
41 } TkCursor;
42
43 /*
44 * Hash table to map from a textual description of a cursor to the
45 * TkCursor record for the cursor, and key structure used in that
46 * hash table:
47 */
48
49 static Tcl_HashTable nameTable;
50 typedef struct {
51 Tk_Uid name; /* Textual name for desired cursor. */
52 Display *display; /* Display for which cursor will be used. */
53 } NameKey;
54
55 /*
56 * Hash table to map from a collection of in-core data about a
57 * cursor (bitmap contents, etc.) to a TkCursor structure:
58 */
59
60 static Tcl_HashTable dataTable;
61 typedef struct {
62 char *source; /* Cursor bits. */
63 char *mask; /* Mask bits. */
64 unsigned int width, height; /* Dimensions of cursor (and data
65 * and mask). */
66 int xHot, yHot; /* Location of cursor hot-spot. */
67 Tk_Uid fg, bg; /* Colors for cursor. */
68 Display *display; /* Display on which cursor will be used. */
69 } DataKey;
70
71 /*
72 * Hash table that maps from Cursor identifiers to the TkCursor structure
73 * for the cursor. This table is indexed by Cursor ids, and is used by
74 * Tk_FreeCursor.
75 */
76
77 static Tcl_HashTable idTable;
78
79 static int initialized = 0; /* 0 means static structures haven't been
80 * initialized yet. */
81
82 /*
83 * The table below is used to map from the name of a cursor to its
84 * index in the official cursor font:
85 */
86
87 static struct CursorName {
88 char *name;
89 unsigned int shape;
90 } cursorNames[] = {
91 {"X_cursor", XC_X_cursor},
92 {"arrow", XC_arrow},
93 {"based_arrow_down", XC_based_arrow_down},
94 {"based_arrow_up", XC_based_arrow_up},
95 {"boat", XC_boat},
96 {"bogosity", XC_bogosity},
97 {"bottom_left_corner", XC_bottom_left_corner},
98 {"bottom_right_corner", XC_bottom_right_corner},
99 {"bottom_side", XC_bottom_side},
100 {"bottom_tee", XC_bottom_tee},
101 {"box_spiral", XC_box_spiral},
102 {"center_ptr", XC_center_ptr},
103 {"circle", XC_circle},
104 {"clock", XC_clock},
105 {"coffee_mug", XC_coffee_mug},
106 {"cross", XC_cross},
107 {"cross_reverse", XC_cross_reverse},
108 {"crosshair", XC_crosshair},
109 {"diamond_cross", XC_diamond_cross},
110 {"dot", XC_dot},
111 {"dotbox", XC_dotbox},
112 {"double_arrow", XC_double_arrow},
113 {"draft_large", XC_draft_large},
114 {"draft_small", XC_draft_small},
115 {"draped_box", XC_draped_box},
116 {"exchange", XC_exchange},
117 {"fleur", XC_fleur},
118 {"gobbler", XC_gobbler},
119 {"gumby", XC_gumby},
120 {"hand1", XC_hand1},
121 {"hand2", XC_hand2},
122 {"heart", XC_heart},
123 {"icon", XC_icon},
124 {"iron_cross", XC_iron_cross},
125 {"left_ptr", XC_left_ptr},
126 {"left_side", XC_left_side},
127 {"left_tee", XC_left_tee},
128 {"leftbutton", XC_leftbutton},
129 {"ll_angle", XC_ll_angle},
130 {"lr_angle", XC_lr_angle},
131 {"man", XC_man},
132 {"middlebutton", XC_middlebutton},
133 {"mouse", XC_mouse},
134 {"pencil", XC_pencil},
135 {"pirate", XC_pirate},
136 {"plus", XC_plus},
137 {"question_arrow", XC_question_arrow},
138 {"right_ptr", XC_right_ptr},
139 {"right_side", XC_right_side},
140 {"right_tee", XC_right_tee},
141 {"rightbutton", XC_rightbutton},
142 {"rtl_logo", XC_rtl_logo},
143 {"sailboat", XC_sailboat},
144 {"sb_down_arrow", XC_sb_down_arrow},
145 {"sb_h_double_arrow", XC_sb_h_double_arrow},
146 {"sb_left_arrow", XC_sb_left_arrow},
147 {"sb_right_arrow", XC_sb_right_arrow},
148 {"sb_up_arrow", XC_sb_up_arrow},
149 {"sb_v_double_arrow", XC_sb_v_double_arrow},
150 {"shuttle", XC_shuttle},
151 {"sizing", XC_sizing},
152 {"spider", XC_spider},
153 {"spraycan", XC_spraycan},
154 {"star", XC_star},
155 {"target", XC_target},
156 {"tcross", XC_tcross},
157 {"top_left_arrow", XC_top_left_arrow},
158 {"top_left_corner", XC_top_left_corner},
159 {"top_right_corner", XC_top_right_corner},
160 {"top_side", XC_top_side},
161 {"top_tee", XC_top_tee},
162 {"trek", XC_trek},
163 {"ul_angle", XC_ul_angle},
164 {"umbrella", XC_umbrella},
165 {"ur_angle", XC_ur_angle},
166 {"watch", XC_watch},
167 {"xterm", XC_xterm},
168 {NULL, 0}
169 };
170
171 /*
172 * Font to use for cursors:
173 */
174
175 #ifndef CURSORFONT
176 #define CURSORFONT "cursor"
177 #endif
178
179 /*
180 * Forward declarations for procedures defined in this file:
181 */
182
183 static void CursorInit _ANSI_ARGS_((void));
184 \f
185 /*
186 *----------------------------------------------------------------------
187 *
188 * Tk_GetCursor --
189 *
190 * Given a string describing a cursor, locate (or create if necessary)
191 * a cursor that fits the description.
192 *
193 * Results:
194 * The return value is the X identifer for the desired cursor,
195 * unless string couldn't be parsed correctly. In this case,
196 * None is returned and an error message is left in interp->result.
197 * The caller should never modify the cursor that is returned, and
198 * should eventually call Tk_FreeCursor when the cursor is no longer
199 * needed.
200 *
201 * Side effects:
202 * The cursor is added to an internal database with a reference count.
203 * For each call to this procedure, there should eventually be a call
204 * to Tk_FreeCursor, so that the database can be cleaned up when cursors
205 * aren't needed anymore.
206 *
207 *----------------------------------------------------------------------
208 */
209
210 Cursor
211 Tk_GetCursor (
212 Tcl_Interp *interp, /* Interpreter to use for error reporting. */
213 Tk_Window tkwin, /* Window in which cursor will be used. */
214 Tk_Uid string /* Description of cursor. See manual entry
215 * for details on legal syntax. */
216 )
217 {
218 NameKey key;
219 Tcl_HashEntry *nameHashPtr, *idHashPtr;
220 register TkCursor *cursorPtr;
221 int new;
222 Cursor cursor;
223 int argc;
224 char **argv = NULL;
225 Pixmap source = None;
226 Pixmap mask = None;
227
228 if (!initialized) {
229 CursorInit();
230 }
231
232 key.name = string;
233 key.display = Tk_Display(tkwin);
234 nameHashPtr = Tcl_CreateHashEntry(&nameTable, (char *) &key, &new);
235 if (!new) {
236 cursorPtr = (TkCursor *) Tcl_GetHashValue(nameHashPtr);
237 cursorPtr->refCount++;
238 return cursorPtr->cursor;
239 }
240
241 /*
242 * No suitable cursor exists. Parse the cursor name into fields
243 * and create a cursor, either from the standard cursor font or
244 * from bitmap files.
245 */
246
247 if (Tcl_SplitList(interp, string, &argc, &argv) != TCL_OK) {
248 goto error;
249 }
250 if (argc == 0) {
251 badString:
252 Tcl_AppendResult(interp, "bad cursor spec \"", string, "\"",
253 (char *) NULL);
254 goto error;
255 }
256 if (argv[0][0] != '@') {
257 XColor fg, bg;
258 int maskIndex;
259 register struct CursorName *namePtr;
260 TkDisplay *dispPtr;
261
262 /*
263 * The cursor is to come from the standard cursor font. If one
264 * arg, it is cursor name (use black and white for fg and bg).
265 * If two args, they are name and fg color (ignore mask). If
266 * three args, they are name, fg, bg. Some of the code below
267 * is stolen from the XCreateFontCursor Xlib procedure.
268 */
269
270 if (argc > 3) {
271 goto badString;
272 }
273 for (namePtr = cursorNames; ; namePtr++) {
274 if (namePtr->name == NULL) {
275 goto badString;
276 }
277 if ((namePtr->name[0] == argv[0][0])
278 && (strcmp(namePtr->name, argv[0]) == 0)) {
279 break;
280 }
281 }
282 maskIndex = namePtr->shape + 1;
283 if (argc == 1) {
284 fg.red = fg.green = fg.blue = 0;
285 bg.red = bg.green = bg.blue = 65535;
286 } else {
287 if (XParseColor(key.display,
288 Tk_DefaultColormap(Tk_Screen(tkwin)),
289 argv[1], &fg) == 0) {
290 Tcl_AppendResult(interp, "invalid color name \"", argv[1],
291 "\"", (char *) NULL);
292 goto error;
293 }
294 if (argc == 2) {
295 bg.red = bg.green = bg.blue = 0;
296 maskIndex = namePtr->shape;
297 } else {
298 if (XParseColor(key.display,
299 Tk_DefaultColormap(Tk_Screen(tkwin)),
300 argv[2], &bg) == 0) {
301 Tcl_AppendResult(interp, "invalid color name \"", argv[2],
302 "\"", (char *) NULL);
303 goto error;
304 }
305 }
306 }
307 dispPtr = ((TkWindow *) tkwin)->dispPtr;
308 if (dispPtr->cursorFont == None) {
309 dispPtr->cursorFont = XLoadFont(key.display, CURSORFONT);
310 if (dispPtr->cursorFont == None) {
311 interp->result = "couldn't load cursor font";
312 goto error;
313 }
314 }
315 cursor = XCreateGlyphCursor(key.display, dispPtr->cursorFont,
316 dispPtr->cursorFont, namePtr->shape, maskIndex,
317 &fg, &bg);
318 } else {
319 unsigned int width, height, maskWidth, maskHeight;
320 int xHot, yHot, dummy1, dummy2;
321 XColor fg, bg;
322
323 /*
324 * The cursor is to be created by reading bitmap files. There
325 * should be either two elements in the list (source, color) or
326 * four (source mask fg bg).
327 */
328
329 if ((argc != 2) && (argc != 4)) {
330 goto badString;
331 }
332 if (XReadBitmapFile(key.display, RootWindowOfScreen(Tk_Screen(tkwin)),
333 &argv[0][1], &width, &height, &source, &xHot, &yHot)
334 != BitmapSuccess) {
335 Tcl_AppendResult(interp, "error reading bitmap file \"",
336 &argv[0][1], "\"", (char *) NULL);
337 goto error;
338 }
339 if ((xHot < 0) || (yHot < 0) || (xHot >= width) || (yHot >= height)) {
340 Tcl_AppendResult(interp, "bad hot spot in bitmap file \"",
341 &argv[0][1], "\"", (char *) NULL);
342 goto error;
343 }
344 if (argc == 2) {
345 if (XParseColor(key.display,
346 Tk_DefaultColormap(Tk_Screen(tkwin)),
347 argv[1], &fg) == 0) {
348 Tcl_AppendResult(interp, "invalid color name \"",
349 argv[1], "\"", (char *) NULL);
350 goto error;
351 }
352 cursor = XCreatePixmapCursor(key.display, source, source,
353 &fg, &fg, xHot, yHot);
354 } else {
355 if (XReadBitmapFile(key.display,
356 RootWindowOfScreen(Tk_Screen(tkwin)), argv[1],
357 &maskWidth, &maskHeight, &mask, &dummy1,
358 &dummy2) != BitmapSuccess) {
359 Tcl_AppendResult(interp, "error reading bitmap file \"",
360 argv[1], "\"", (char *) NULL);
361 goto error;
362 }
363 if ((maskWidth != width) && (maskHeight != height)) {
364 interp->result =
365 "source and mask bitmaps have different sizes";
366 goto error;
367 }
368 if (XParseColor(key.display,
369 Tk_DefaultColormap(Tk_Screen(tkwin)),
370 argv[2], &fg) == 0) {
371 Tcl_AppendResult(interp, "invalid color name \"", argv[2],
372 "\"", (char *) NULL);
373 goto error;
374 }
375 if (XParseColor(key.display,
376 Tk_DefaultColormap(Tk_Screen(tkwin)),
377 argv[3], &bg) == 0) {
378 Tcl_AppendResult(interp, "invalid color name \"", argv[3],
379 "\"", (char *) NULL);
380 goto error;
381 }
382 cursor = XCreatePixmapCursor(key.display, source, mask,
383 &fg, &bg, xHot, yHot);
384 }
385 }
386 ckfree((char *) argv);
387
388 /*
389 * Add information about this cursor to our database.
390 */
391
392 cursorPtr = (TkCursor *) ckalloc(sizeof(TkCursor));
393 cursorPtr->cursor = cursor;
394 cursorPtr->display = key.display;
395 cursorPtr->refCount = 1;
396 cursorPtr->otherTable = &nameTable;
397 cursorPtr->hashPtr = nameHashPtr;
398 idHashPtr = Tcl_CreateHashEntry(&idTable, (char *) cursorPtr->cursor,
399 &new);
400 if (!new) {
401 /* deh patched to support multiple displays */
402 /* panic("cursor already registered in Tk_GetCursor"); */
403 cursorPtr->refCount = 1000;
404 }
405 Tcl_SetHashValue(nameHashPtr, cursorPtr);
406 Tcl_SetHashValue(idHashPtr, cursorPtr);
407 return cursorPtr->cursor;
408
409 error:
410 Tcl_DeleteHashEntry(nameHashPtr);
411 if (argv != NULL) {
412 ckfree((char *) argv);
413 }
414 if (source != None) {
415 XFreePixmap(key.display, source);
416 }
417 if (mask != None) {
418 XFreePixmap(key.display, mask);
419 }
420 return None;
421 }
422 \f
423 /*
424 *----------------------------------------------------------------------
425 *
426 * Tk_GetCursorFromData --
427 *
428 * Given a description of the bits and colors for a cursor,
429 * make a cursor that has the given properties.
430 *
431 * Results:
432 * The return value is the X identifer for the desired cursor,
433 * unless it couldn't be created properly. In this case, None is
434 * returned and an error message is left in interp->result. The
435 * caller should never modify the cursor that is returned, and
436 * should eventually call Tk_FreeCursor when the cursor is no
437 * longer needed.
438 *
439 * Side effects:
440 * The cursor is added to an internal database with a reference count.
441 * For each call to this procedure, there should eventually be a call
442 * to Tk_FreeCursor, so that the database can be cleaned up when cursors
443 * aren't needed anymore.
444 *
445 *----------------------------------------------------------------------
446 */
447
448 Cursor
449 Tk_GetCursorFromData (
450 Tcl_Interp *interp, /* Interpreter to use for error reporting. */
451 Tk_Window tkwin, /* Window in which cursor will be used. */
452 char *source, /* Bitmap data for cursor shape. */
453 char *mask, /* Bitmap data for cursor mask. */
454 unsigned int width,
455 unsigned int height, /* Dimensions of cursor. */
456 int xHot,
457 int yHot, /* Location of hot-spot in cursor. */
458 Tk_Uid fg, /* Foreground color for cursor. */
459 Tk_Uid bg /* Background color for cursor. */
460 )
461 {
462 DataKey key;
463 Tcl_HashEntry *dataHashPtr, *idHashPtr;
464 register TkCursor *cursorPtr;
465 int new;
466 XColor fgColor, bgColor;
467 Pixmap sourcePixmap, maskPixmap;
468
469 if (!initialized) {
470 CursorInit();
471 }
472
473 key.source = source;
474 key.mask = mask;
475 key.width = width;
476 key.height = height;
477 key.xHot = xHot;
478 key.yHot = yHot;
479 key.fg = fg;
480 key.bg = bg;
481 key.display = Tk_Display(tkwin);
482 dataHashPtr = Tcl_CreateHashEntry(&dataTable, (char *) &key, &new);
483 if (!new) {
484 cursorPtr = (TkCursor *) Tcl_GetHashValue(dataHashPtr);
485 cursorPtr->refCount++;
486 return cursorPtr->cursor;
487 }
488
489 /*
490 * No suitable cursor exists yet. Make one using the data
491 * available and add it to the database.
492 */
493
494 if (XParseColor(key.display, Tk_DefaultColormap(Tk_Screen(tkwin)),
495 fg, &fgColor) == 0) {
496 Tcl_AppendResult(interp, "invalid color name \"", fg, "\"",
497 (char *) NULL);
498 goto error;
499 }
500 if (XParseColor(key.display, Tk_DefaultColormap(Tk_Screen(tkwin)),
501 bg, &bgColor) == 0) {
502 Tcl_AppendResult(interp, "invalid color name \"", bg, "\"",
503 (char *) NULL);
504 goto error;
505 }
506
507 cursorPtr = (TkCursor *) ckalloc(sizeof(TkCursor));
508 sourcePixmap = XCreateBitmapFromData(key.display,
509 RootWindowOfScreen(Tk_Screen(tkwin)), source, width, height);
510 maskPixmap = XCreateBitmapFromData(key.display,
511 RootWindowOfScreen(Tk_Screen(tkwin)), mask, width, height);
512 cursorPtr->cursor = XCreatePixmapCursor(key.display, sourcePixmap,
513 maskPixmap, &fgColor, &bgColor, xHot, yHot);
514 XFreePixmap(key.display, sourcePixmap);
515 XFreePixmap(key.display, maskPixmap);
516 cursorPtr->display = key.display;
517 cursorPtr->refCount = 1;
518 cursorPtr->otherTable = &dataTable;
519 cursorPtr->hashPtr = dataHashPtr;
520 idHashPtr = Tcl_CreateHashEntry(&idTable, (char *) cursorPtr->cursor, &new);
521 if (!new) {
522 /* deh patched to support multiple displays */
523 /* panic("cursor already registered in Tk_GetCursorFromData"); */
524 cursorPtr->refCount = 1000;
525 }
526 Tcl_SetHashValue(dataHashPtr, cursorPtr);
527 Tcl_SetHashValue(idHashPtr, cursorPtr);
528 return cursorPtr->cursor;
529
530 error:
531 Tcl_DeleteHashEntry(dataHashPtr);
532 return None;
533 }
534 \f
535 /*
536 *--------------------------------------------------------------
537 *
538 * Tk_NameOfCursor --
539 *
540 * Given a cursor, return a textual string identifying it.
541 *
542 * Results:
543 * If cursor was created by Tk_GetCursor, then the return
544 * value is the "string" that was used to create it.
545 * Otherwise the return value is a string giving the X
546 * identifier for the cursor. The storage for the returned
547 * string is only guaranteed to persist up until the next
548 * call to this procedure.
549 *
550 * Side effects:
551 * None.
552 *
553 *--------------------------------------------------------------
554 */
555
556 char *
557 Tk_NameOfCursor (
558 Cursor cursor /* Cursor to be released. */
559 )
560 {
561 Tcl_HashEntry *idHashPtr;
562 TkCursor *cursorPtr;
563 static char string[20];
564
565 if (!initialized) {
566 printid:
567 sprintf(string, "cursor id 0x%x", cursor);
568 return string;
569 }
570 idHashPtr = Tcl_FindHashEntry(&idTable, (char *) cursor);
571 if (idHashPtr == NULL) {
572 goto printid;
573 }
574 cursorPtr = (TkCursor *) Tcl_GetHashValue(idHashPtr);
575 if (cursorPtr->otherTable != &nameTable) {
576 goto printid;
577 }
578 return ((NameKey *) cursorPtr->hashPtr->key.words)->name;
579 }
580 \f
581 /*
582 *----------------------------------------------------------------------
583 *
584 * Tk_FreeCursor --
585 *
586 * This procedure is called to release a cursor allocated by
587 * Tk_GetCursor or TkGetCursorFromData.
588 *
589 * Results:
590 * None.
591 *
592 * Side effects:
593 * The reference count associated with cursor is decremented, and
594 * it is officially deallocated if no-one is using it anymore.
595 *
596 *----------------------------------------------------------------------
597 */
598
599 void
600 Tk_FreeCursor (
601 Cursor cursor /* Cursor to be released. */
602 )
603 {
604 Tcl_HashEntry *idHashPtr;
605 register TkCursor *cursorPtr;
606
607 if (!initialized) {
608 panic("Tk_FreeCursor called before Tk_GetCursor");
609 }
610
611 idHashPtr = Tcl_FindHashEntry(&idTable, (char *) cursor);
612 if (idHashPtr == NULL) {
613 panic("Tk_FreeCursor received unknown cursor argument");
614 }
615 cursorPtr = (TkCursor *) Tcl_GetHashValue(idHashPtr);
616 cursorPtr->refCount--;
617 if (cursorPtr->refCount == 0) {
618 XFreeCursor(cursorPtr->display, cursorPtr->cursor);
619 Tcl_DeleteHashEntry(cursorPtr->hashPtr);
620 Tcl_DeleteHashEntry(idHashPtr);
621 ckfree((char *) cursorPtr);
622 }
623 }
624 \f
625 /*
626 *----------------------------------------------------------------------
627 *
628 * CursorInit --
629 *
630 * Initialize the structures used for cursor management.
631 *
632 * Results:
633 * None.
634 *
635 * Side effects:
636 * Read the code.
637 *
638 *----------------------------------------------------------------------
639 */
640
641 static void
642 CursorInit (void)
643 {
644 initialized = 1;
645 Tcl_InitHashTable(&nameTable, sizeof(NameKey)/sizeof(long));
646 Tcl_InitHashTable(&dataTable, sizeof(DataKey)/sizeof(long));
647 Tcl_InitHashTable(&idTable, TCL_ONE_WORD_KEYS);
648 }
Impressum, Datenschutz