]>
cvs.zerfleddert.de Git - micropolis/blob - src/tk/tkcursor.c
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.
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.
19 static char rcsid
[] = "$Header: /user6/ouster/wish/RCS/tkCursor.c,v 1.12 91/10/31 11:40:41 ouster Exp $ SPRITE (Berkeley)";
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.
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). */
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
49 static Tcl_HashTable nameTable
;
51 Tk_Uid name
; /* Textual name for desired cursor. */
52 Display
*display
; /* Display for which cursor will be used. */
56 * Hash table to map from a collection of in-core data about a
57 * cursor (bitmap contents, etc.) to a TkCursor structure:
60 static Tcl_HashTable dataTable
;
62 char *source
; /* Cursor bits. */
63 char *mask
; /* Mask bits. */
64 unsigned int width
, height
; /* Dimensions of cursor (and data
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. */
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
77 static Tcl_HashTable idTable
;
79 static int initialized
= 0; /* 0 means static structures haven't been
83 * The table below is used to map from the name of a cursor to its
84 * index in the official cursor font:
87 static struct CursorName
{
91 {"X_cursor", XC_X_cursor
},
93 {"based_arrow_down", XC_based_arrow_down
},
94 {"based_arrow_up", XC_based_arrow_up
},
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
},
105 {"coffee_mug", XC_coffee_mug
},
107 {"cross_reverse", XC_cross_reverse
},
108 {"crosshair", XC_crosshair
},
109 {"diamond_cross", XC_diamond_cross
},
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
},
118 {"gobbler", XC_gobbler
},
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
},
132 {"middlebutton", XC_middlebutton
},
134 {"pencil", XC_pencil
},
135 {"pirate", XC_pirate
},
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
},
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
},
163 {"ul_angle", XC_ul_angle
},
164 {"umbrella", XC_umbrella
},
165 {"ur_angle", XC_ur_angle
},
172 * Font to use for cursors:
176 #define CURSORFONT "cursor"
180 * Forward declarations for procedures defined in this file:
183 static void CursorInit
_ANSI_ARGS_((void));
186 *----------------------------------------------------------------------
190 * Given a string describing a cursor, locate (or create if necessary)
191 * a cursor that fits the description.
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
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.
207 *----------------------------------------------------------------------
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. */
219 Tcl_HashEntry
*nameHashPtr
, *idHashPtr
;
220 register TkCursor
*cursorPtr
;
225 Pixmap source
= None
;
233 key
.display
= Tk_Display(tkwin
);
234 nameHashPtr
= Tcl_CreateHashEntry(&nameTable
, (char *) &key
, &new);
236 cursorPtr
= (TkCursor
*) Tcl_GetHashValue(nameHashPtr
);
237 cursorPtr
->refCount
++;
238 return cursorPtr
->cursor
;
242 * No suitable cursor exists. Parse the cursor name into fields
243 * and create a cursor, either from the standard cursor font or
247 if (Tcl_SplitList(interp
, string
, &argc
, &argv
) != TCL_OK
) {
252 Tcl_AppendResult(interp
, "bad cursor spec \"", string
, "\"",
256 if (argv
[0][0] != '@') {
259 register struct CursorName
*namePtr
;
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.
273 for (namePtr
= cursorNames
; ; namePtr
++) {
274 if (namePtr
->name
== NULL
) {
277 if ((namePtr
->name
[0] == argv
[0][0])
278 && (strcmp(namePtr
->name
, argv
[0]) == 0)) {
282 maskIndex
= namePtr
->shape
+ 1;
284 fg
.red
= fg
.green
= fg
.blue
= 0;
285 bg
.red
= bg
.green
= bg
.blue
= 65535;
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
);
295 bg
.red
= bg
.green
= bg
.blue
= 0;
296 maskIndex
= namePtr
->shape
;
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
);
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";
315 cursor
= XCreateGlyphCursor(key
.display
, dispPtr
->cursorFont
,
316 dispPtr
->cursorFont
, namePtr
->shape
, maskIndex
,
319 unsigned int width
, height
, maskWidth
, maskHeight
;
320 int xHot
, yHot
, dummy1
, dummy2
;
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).
329 if ((argc
!= 2) && (argc
!= 4)) {
332 if (XReadBitmapFile(key
.display
, RootWindowOfScreen(Tk_Screen(tkwin
)),
333 &argv
[0][1], &width
, &height
, &source
, &xHot
, &yHot
)
335 Tcl_AppendResult(interp
, "error reading bitmap file \"",
336 &argv
[0][1], "\"", (char *) NULL
);
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
);
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
);
352 cursor
= XCreatePixmapCursor(key
.display
, source
, source
,
353 &fg
, &fg
, xHot
, yHot
);
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
);
363 if ((maskWidth
!= width
) && (maskHeight
!= height
)) {
365 "source and mask bitmaps have different sizes";
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
);
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
);
382 cursor
= XCreatePixmapCursor(key
.display
, source
, mask
,
383 &fg
, &bg
, xHot
, yHot
);
386 ckfree((char *) argv
);
389 * Add information about this cursor to our database.
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
,
401 /* deh patched to support multiple displays */
402 /* panic("cursor already registered in Tk_GetCursor"); */
403 cursorPtr
->refCount
= 1000;
405 Tcl_SetHashValue(nameHashPtr
, cursorPtr
);
406 Tcl_SetHashValue(idHashPtr
, cursorPtr
);
407 return cursorPtr
->cursor
;
410 Tcl_DeleteHashEntry(nameHashPtr
);
412 ckfree((char *) argv
);
414 if (source
!= None
) {
415 XFreePixmap(key
.display
, source
);
418 XFreePixmap(key
.display
, mask
);
424 *----------------------------------------------------------------------
426 * Tk_GetCursorFromData --
428 * Given a description of the bits and colors for a cursor,
429 * make a cursor that has the given properties.
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
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.
445 *----------------------------------------------------------------------
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. */
455 unsigned int height
, /* Dimensions of cursor. */
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. */
463 Tcl_HashEntry
*dataHashPtr
, *idHashPtr
;
464 register TkCursor
*cursorPtr
;
466 XColor fgColor
, bgColor
;
467 Pixmap sourcePixmap
, maskPixmap
;
481 key
.display
= Tk_Display(tkwin
);
482 dataHashPtr
= Tcl_CreateHashEntry(&dataTable
, (char *) &key
, &new);
484 cursorPtr
= (TkCursor
*) Tcl_GetHashValue(dataHashPtr
);
485 cursorPtr
->refCount
++;
486 return cursorPtr
->cursor
;
490 * No suitable cursor exists yet. Make one using the data
491 * available and add it to the database.
494 if (XParseColor(key
.display
, Tk_DefaultColormap(Tk_Screen(tkwin
)),
495 fg
, &fgColor
) == 0) {
496 Tcl_AppendResult(interp
, "invalid color name \"", fg
, "\"",
500 if (XParseColor(key
.display
, Tk_DefaultColormap(Tk_Screen(tkwin
)),
501 bg
, &bgColor
) == 0) {
502 Tcl_AppendResult(interp
, "invalid color name \"", bg
, "\"",
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);
522 /* deh patched to support multiple displays */
523 /* panic("cursor already registered in Tk_GetCursorFromData"); */
524 cursorPtr
->refCount
= 1000;
526 Tcl_SetHashValue(dataHashPtr
, cursorPtr
);
527 Tcl_SetHashValue(idHashPtr
, cursorPtr
);
528 return cursorPtr
->cursor
;
531 Tcl_DeleteHashEntry(dataHashPtr
);
536 *--------------------------------------------------------------
540 * Given a cursor, return a textual string identifying it.
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.
553 *--------------------------------------------------------------
558 Cursor cursor
/* Cursor to be released. */
561 Tcl_HashEntry
*idHashPtr
;
563 static char string
[20];
567 sprintf(string
, "cursor id 0x%x", cursor
);
570 idHashPtr
= Tcl_FindHashEntry(&idTable
, (char *) cursor
);
571 if (idHashPtr
== NULL
) {
574 cursorPtr
= (TkCursor
*) Tcl_GetHashValue(idHashPtr
);
575 if (cursorPtr
->otherTable
!= &nameTable
) {
578 return ((NameKey
*) cursorPtr
->hashPtr
->key
.words
)->name
;
582 *----------------------------------------------------------------------
586 * This procedure is called to release a cursor allocated by
587 * Tk_GetCursor or TkGetCursorFromData.
593 * The reference count associated with cursor is decremented, and
594 * it is officially deallocated if no-one is using it anymore.
596 *----------------------------------------------------------------------
601 Cursor cursor
/* Cursor to be released. */
604 Tcl_HashEntry
*idHashPtr
;
605 register TkCursor
*cursorPtr
;
608 panic("Tk_FreeCursor called before Tk_GetCursor");
611 idHashPtr
= Tcl_FindHashEntry(&idTable
, (char *) cursor
);
612 if (idHashPtr
== NULL
) {
613 panic("Tk_FreeCursor received unknown cursor argument");
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
);
626 *----------------------------------------------------------------------
630 * Initialize the structures used for cursor management.
638 *----------------------------------------------------------------------
645 Tcl_InitHashTable(&nameTable
, sizeof(NameKey
)/sizeof(long));
646 Tcl_InitHashTable(&dataTable
, sizeof(DataKey
)/sizeof(long));
647 Tcl_InitHashTable(&idTable
, TCL_ONE_WORD_KEYS
);