]>
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 *----------------------------------------------------------------------
211 Tk_GetCursor(interp
, tkwin
, string
)
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. */
218 Tcl_HashEntry
*nameHashPtr
, *idHashPtr
;
219 register TkCursor
*cursorPtr
;
224 Pixmap source
= None
;
232 key
.display
= Tk_Display(tkwin
);
233 nameHashPtr
= Tcl_CreateHashEntry(&nameTable
, (char *) &key
, &new);
235 cursorPtr
= (TkCursor
*) Tcl_GetHashValue(nameHashPtr
);
236 cursorPtr
->refCount
++;
237 return cursorPtr
->cursor
;
241 * No suitable cursor exists. Parse the cursor name into fields
242 * and create a cursor, either from the standard cursor font or
246 if (Tcl_SplitList(interp
, string
, &argc
, &argv
) != TCL_OK
) {
251 Tcl_AppendResult(interp
, "bad cursor spec \"", string
, "\"",
255 if (argv
[0][0] != '@') {
258 register struct CursorName
*namePtr
;
262 * The cursor is to come from the standard cursor font. If one
263 * arg, it is cursor name (use black and white for fg and bg).
264 * If two args, they are name and fg color (ignore mask). If
265 * three args, they are name, fg, bg. Some of the code below
266 * is stolen from the XCreateFontCursor Xlib procedure.
272 for (namePtr
= cursorNames
; ; namePtr
++) {
273 if (namePtr
->name
== NULL
) {
276 if ((namePtr
->name
[0] == argv
[0][0])
277 && (strcmp(namePtr
->name
, argv
[0]) == 0)) {
281 maskIndex
= namePtr
->shape
+ 1;
283 fg
.red
= fg
.green
= fg
.blue
= 0;
284 bg
.red
= bg
.green
= bg
.blue
= 65535;
286 if (XParseColor(key
.display
,
287 Tk_DefaultColormap(Tk_Screen(tkwin
)),
288 argv
[1], &fg
) == 0) {
289 Tcl_AppendResult(interp
, "invalid color name \"", argv
[1],
290 "\"", (char *) NULL
);
294 bg
.red
= bg
.green
= bg
.blue
= 0;
295 maskIndex
= namePtr
->shape
;
297 if (XParseColor(key
.display
,
298 Tk_DefaultColormap(Tk_Screen(tkwin
)),
299 argv
[2], &bg
) == 0) {
300 Tcl_AppendResult(interp
, "invalid color name \"", argv
[2],
301 "\"", (char *) NULL
);
306 dispPtr
= ((TkWindow
*) tkwin
)->dispPtr
;
307 if (dispPtr
->cursorFont
== None
) {
308 dispPtr
->cursorFont
= XLoadFont(key
.display
, CURSORFONT
);
309 if (dispPtr
->cursorFont
== None
) {
310 interp
->result
= "couldn't load cursor font";
314 cursor
= XCreateGlyphCursor(key
.display
, dispPtr
->cursorFont
,
315 dispPtr
->cursorFont
, namePtr
->shape
, maskIndex
,
318 unsigned int width
, height
, maskWidth
, maskHeight
;
319 int xHot
, yHot
, dummy1
, dummy2
;
323 * The cursor is to be created by reading bitmap files. There
324 * should be either two elements in the list (source, color) or
325 * four (source mask fg bg).
328 if ((argc
!= 2) && (argc
!= 4)) {
331 if (XReadBitmapFile(key
.display
, RootWindowOfScreen(Tk_Screen(tkwin
)),
332 &argv
[0][1], &width
, &height
, &source
, &xHot
, &yHot
)
334 Tcl_AppendResult(interp
, "error reading bitmap file \"",
335 &argv
[0][1], "\"", (char *) NULL
);
338 if ((xHot
< 0) || (yHot
< 0) || (xHot
>= width
) || (yHot
>= height
)) {
339 Tcl_AppendResult(interp
, "bad hot spot in bitmap file \"",
340 &argv
[0][1], "\"", (char *) NULL
);
344 if (XParseColor(key
.display
,
345 Tk_DefaultColormap(Tk_Screen(tkwin
)),
346 argv
[1], &fg
) == 0) {
347 Tcl_AppendResult(interp
, "invalid color name \"",
348 argv
[1], "\"", (char *) NULL
);
351 cursor
= XCreatePixmapCursor(key
.display
, source
, source
,
352 &fg
, &fg
, xHot
, yHot
);
354 if (XReadBitmapFile(key
.display
,
355 RootWindowOfScreen(Tk_Screen(tkwin
)), argv
[1],
356 &maskWidth
, &maskHeight
, &mask
, &dummy1
,
357 &dummy2
) != BitmapSuccess
) {
358 Tcl_AppendResult(interp
, "error reading bitmap file \"",
359 argv
[1], "\"", (char *) NULL
);
362 if ((maskWidth
!= width
) && (maskHeight
!= height
)) {
364 "source and mask bitmaps have different sizes";
367 if (XParseColor(key
.display
,
368 Tk_DefaultColormap(Tk_Screen(tkwin
)),
369 argv
[2], &fg
) == 0) {
370 Tcl_AppendResult(interp
, "invalid color name \"", argv
[2],
371 "\"", (char *) NULL
);
374 if (XParseColor(key
.display
,
375 Tk_DefaultColormap(Tk_Screen(tkwin
)),
376 argv
[3], &bg
) == 0) {
377 Tcl_AppendResult(interp
, "invalid color name \"", argv
[3],
378 "\"", (char *) NULL
);
381 cursor
= XCreatePixmapCursor(key
.display
, source
, mask
,
382 &fg
, &bg
, xHot
, yHot
);
385 ckfree((char *) argv
);
388 * Add information about this cursor to our database.
391 cursorPtr
= (TkCursor
*) ckalloc(sizeof(TkCursor
));
392 cursorPtr
->cursor
= cursor
;
393 cursorPtr
->display
= key
.display
;
394 cursorPtr
->refCount
= 1;
395 cursorPtr
->otherTable
= &nameTable
;
396 cursorPtr
->hashPtr
= nameHashPtr
;
397 idHashPtr
= Tcl_CreateHashEntry(&idTable
, (char *) cursorPtr
->cursor
,
400 /* deh patched to support multiple displays */
401 /* panic("cursor already registered in Tk_GetCursor"); */
402 cursorPtr
->refCount
= 1000;
404 Tcl_SetHashValue(nameHashPtr
, cursorPtr
);
405 Tcl_SetHashValue(idHashPtr
, cursorPtr
);
406 return cursorPtr
->cursor
;
409 Tcl_DeleteHashEntry(nameHashPtr
);
411 ckfree((char *) argv
);
413 if (source
!= None
) {
414 XFreePixmap(key
.display
, source
);
417 XFreePixmap(key
.display
, mask
);
423 *----------------------------------------------------------------------
425 * Tk_GetCursorFromData --
427 * Given a description of the bits and colors for a cursor,
428 * make a cursor that has the given properties.
431 * The return value is the X identifer for the desired cursor,
432 * unless it couldn't be created properly. In this case, None is
433 * returned and an error message is left in interp->result. The
434 * caller should never modify the cursor that is returned, and
435 * should eventually call Tk_FreeCursor when the cursor is no
439 * The cursor is added to an internal database with a reference count.
440 * For each call to this procedure, there should eventually be a call
441 * to Tk_FreeCursor, so that the database can be cleaned up when cursors
442 * aren't needed anymore.
444 *----------------------------------------------------------------------
448 Tk_GetCursorFromData(interp
, tkwin
, source
, mask
, width
, height
,
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
, height
; /* Dimensions of cursor. */
455 int xHot
, yHot
; /* Location of hot-spot in cursor. */
456 Tk_Uid fg
; /* Foreground color for cursor. */
457 Tk_Uid bg
; /* Background color for cursor. */
460 Tcl_HashEntry
*dataHashPtr
, *idHashPtr
;
461 register TkCursor
*cursorPtr
;
463 XColor fgColor
, bgColor
;
464 Pixmap sourcePixmap
, maskPixmap
;
478 key
.display
= Tk_Display(tkwin
);
479 dataHashPtr
= Tcl_CreateHashEntry(&dataTable
, (char *) &key
, &new);
481 cursorPtr
= (TkCursor
*) Tcl_GetHashValue(dataHashPtr
);
482 cursorPtr
->refCount
++;
483 return cursorPtr
->cursor
;
487 * No suitable cursor exists yet. Make one using the data
488 * available and add it to the database.
491 if (XParseColor(key
.display
, Tk_DefaultColormap(Tk_Screen(tkwin
)),
492 fg
, &fgColor
) == 0) {
493 Tcl_AppendResult(interp
, "invalid color name \"", fg
, "\"",
497 if (XParseColor(key
.display
, Tk_DefaultColormap(Tk_Screen(tkwin
)),
498 bg
, &bgColor
) == 0) {
499 Tcl_AppendResult(interp
, "invalid color name \"", bg
, "\"",
504 cursorPtr
= (TkCursor
*) ckalloc(sizeof(TkCursor
));
505 sourcePixmap
= XCreateBitmapFromData(key
.display
,
506 RootWindowOfScreen(Tk_Screen(tkwin
)), source
, width
, height
);
507 maskPixmap
= XCreateBitmapFromData(key
.display
,
508 RootWindowOfScreen(Tk_Screen(tkwin
)), mask
, width
, height
);
509 cursorPtr
->cursor
= XCreatePixmapCursor(key
.display
, sourcePixmap
,
510 maskPixmap
, &fgColor
, &bgColor
, xHot
, yHot
);
511 XFreePixmap(key
.display
, sourcePixmap
);
512 XFreePixmap(key
.display
, maskPixmap
);
513 cursorPtr
->display
= key
.display
;
514 cursorPtr
->refCount
= 1;
515 cursorPtr
->otherTable
= &dataTable
;
516 cursorPtr
->hashPtr
= dataHashPtr
;
517 idHashPtr
= Tcl_CreateHashEntry(&idTable
, (char *) cursorPtr
->cursor
, &new);
519 /* deh patched to support multiple displays */
520 /* panic("cursor already registered in Tk_GetCursorFromData"); */
521 cursorPtr
->refCount
= 1000;
523 Tcl_SetHashValue(dataHashPtr
, cursorPtr
);
524 Tcl_SetHashValue(idHashPtr
, cursorPtr
);
525 return cursorPtr
->cursor
;
528 Tcl_DeleteHashEntry(dataHashPtr
);
533 *--------------------------------------------------------------
537 * Given a cursor, return a textual string identifying it.
540 * If cursor was created by Tk_GetCursor, then the return
541 * value is the "string" that was used to create it.
542 * Otherwise the return value is a string giving the X
543 * identifier for the cursor. The storage for the returned
544 * string is only guaranteed to persist up until the next
545 * call to this procedure.
550 *--------------------------------------------------------------
554 Tk_NameOfCursor(cursor
)
555 Cursor cursor
; /* Cursor to be released. */
557 Tcl_HashEntry
*idHashPtr
;
559 static char string
[20];
563 sprintf(string
, "cursor id 0x%x", cursor
);
566 idHashPtr
= Tcl_FindHashEntry(&idTable
, (char *) cursor
);
567 if (idHashPtr
== NULL
) {
570 cursorPtr
= (TkCursor
*) Tcl_GetHashValue(idHashPtr
);
571 if (cursorPtr
->otherTable
!= &nameTable
) {
574 return ((NameKey
*) cursorPtr
->hashPtr
->key
.words
)->name
;
578 *----------------------------------------------------------------------
582 * This procedure is called to release a cursor allocated by
583 * Tk_GetCursor or TkGetCursorFromData.
589 * The reference count associated with cursor is decremented, and
590 * it is officially deallocated if no-one is using it anymore.
592 *----------------------------------------------------------------------
596 Tk_FreeCursor(cursor
)
597 Cursor cursor
; /* Cursor to be released. */
599 Tcl_HashEntry
*idHashPtr
;
600 register TkCursor
*cursorPtr
;
603 panic("Tk_FreeCursor called before Tk_GetCursor");
606 idHashPtr
= Tcl_FindHashEntry(&idTable
, (char *) cursor
);
607 if (idHashPtr
== NULL
) {
608 panic("Tk_FreeCursor received unknown cursor argument");
610 cursorPtr
= (TkCursor
*) Tcl_GetHashValue(idHashPtr
);
611 cursorPtr
->refCount
--;
612 if (cursorPtr
->refCount
== 0) {
613 XFreeCursor(cursorPtr
->display
, cursorPtr
->cursor
);
614 Tcl_DeleteHashEntry(cursorPtr
->hashPtr
);
615 Tcl_DeleteHashEntry(idHashPtr
);
616 ckfree((char *) cursorPtr
);
621 *----------------------------------------------------------------------
625 * Initialize the structures used for cursor management.
633 *----------------------------------------------------------------------
640 Tcl_InitHashTable(&nameTable
, sizeof(NameKey
)/sizeof(long));
641 Tcl_InitHashTable(&dataTable
, sizeof(DataKey
)/sizeof(long));
642 Tcl_InitHashTable(&idTable
, TCL_ONE_WORD_KEYS
);