cursor.c revision 645f5050
1645f5050Syouri/*
2645f5050Syouri * Copyright 1989 Massachusetts Institute of Technology
3645f5050Syouri *
4645f5050Syouri * Permission to use, copy, modify, and distribute this software and its
5645f5050Syouri * documentation for any purpose and without fee is hereby granted, provided
6645f5050Syouri * that the above copyright notice appear in all copies and that both that
7645f5050Syouri * copyright notice and this permission notice appear in supporting
8645f5050Syouri * documentation, and that the name of M.I.T. not be used in advertising
9645f5050Syouri * or publicity pertaining to distribution of the software without specific,
10645f5050Syouri * written prior permission.  M.I.T. makes no representations about the
11645f5050Syouri * suitability of this software for any purpose.  It is provided "as is"
12645f5050Syouri * without express or implied warranty.
13645f5050Syouri *
14645f5050Syouri * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
15645f5050Syouri * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
16645f5050Syouri * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17645f5050Syouri * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
18645f5050Syouri * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
19645f5050Syouri * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20645f5050Syouri */
21645f5050Syouri/*
22645f5050Syouri *  [ ctwm ]
23645f5050Syouri *
24645f5050Syouri *  Copyright 1992 Claude Lecommandeur.
25645f5050Syouri *
26645f5050Syouri * Permission to use, copy, modify  and distribute this software  [ctwm] and
27645f5050Syouri * its documentation for any purpose is hereby granted without fee, provided
28645f5050Syouri * that the above  copyright notice appear  in all copies and that both that
29645f5050Syouri * copyright notice and this permission notice appear in supporting documen-
30645f5050Syouri * tation, and that the name of  Claude Lecommandeur not be used in adverti-
31645f5050Syouri * sing or  publicity  pertaining to  distribution of  the software  without
32645f5050Syouri * specific, written prior permission. Claude Lecommandeur make no represen-
33645f5050Syouri * tations  about the suitability  of this software  for any purpose.  It is
34645f5050Syouri * provided "as is" without express or implied warranty.
35645f5050Syouri *
36645f5050Syouri * Claude Lecommandeur DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
37645f5050Syouri * INCLUDING ALL  IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS.  IN NO
38645f5050Syouri * EVENT SHALL  Claude Lecommandeur  BE LIABLE FOR ANY SPECIAL,  INDIRECT OR
39645f5050Syouri * CONSEQUENTIAL  DAMAGES OR ANY  DAMAGES WHATSOEVER  RESULTING FROM LOSS OF
40645f5050Syouri * USE, DATA  OR PROFITS,  WHETHER IN AN ACTION  OF CONTRACT,  NEGLIGENCE OR
41645f5050Syouri * OTHER  TORTIOUS ACTION,  ARISING OUT OF OR IN  CONNECTION WITH THE USE OR
42645f5050Syouri * PERFORMANCE OF THIS SOFTWARE.
43645f5050Syouri *
44645f5050Syouri * Author:  Claude Lecommandeur [ lecom@sic.epfl.ch ][ April 1992 ]
45645f5050Syouri */
46645f5050Syouri
47645f5050Syouri/***********************************************************************
48645f5050Syouri *
49645f5050Syouri * $XConsortium: cursor.c,v 1.10 89/12/14 14:52:23 jim Exp $
50645f5050Syouri *
51645f5050Syouri * cursor creation code
52645f5050Syouri *
53645f5050Syouri * 05-Apr-89 Thomas E. LaStrange	File created
54645f5050Syouri *
55645f5050Syouri * Do the necessary modification to be integrated in ctwm.
56645f5050Syouri * Can no longer be used for the standard twm.
57645f5050Syouri *
58645f5050Syouri * 22-April-92 Claude Lecommandeur.
59645f5050Syouri *
60645f5050Syouri *
61645f5050Syouri ***********************************************************************/
62645f5050Syouri
63645f5050Syouri#include <stdio.h>
64645f5050Syouri#include "twm.h"
65645f5050Syouri#ifdef VMS
66645f5050Syouri#include <decw$include/Xos.h>
67645f5050Syouri#else
68645f5050Syouri#include <X11/Xos.h>
69645f5050Syouri#endif
70645f5050Syouri#include "screen.h"
71645f5050Syouri#include "util.h"
72645f5050Syouri#include "cursor.h"
73645f5050Syouri
74645f5050Syouristatic struct _CursorName {
75645f5050Syouri    char		*name;
76645f5050Syouri    unsigned int	shape;
77645f5050Syouri    Cursor		cursor;
78645f5050Syouri} cursor_names[] = {
79645f5050Syouri
80645f5050Syouri{"X_cursor",		XC_X_cursor,		None},
81645f5050Syouri{"arrow",		XC_arrow,		None},
82645f5050Syouri{"based_arrow_down",	XC_based_arrow_down,    None},
83645f5050Syouri{"based_arrow_up",	XC_based_arrow_up,      None},
84645f5050Syouri{"boat",		XC_boat,		None},
85645f5050Syouri{"bogosity",		XC_bogosity,		None},
86645f5050Syouri{"bottom_left_corner",	XC_bottom_left_corner,  None},
87645f5050Syouri{"bottom_right_corner",	XC_bottom_right_corner, None},
88645f5050Syouri{"bottom_side",		XC_bottom_side,		None},
89645f5050Syouri{"bottom_tee",		XC_bottom_tee,		None},
90645f5050Syouri{"box_spiral",		XC_box_spiral,		None},
91645f5050Syouri{"center_ptr",		XC_center_ptr,		None},
92645f5050Syouri{"circle",		XC_circle,		None},
93645f5050Syouri{"clock",		XC_clock,		None},
94645f5050Syouri{"coffee_mug",		XC_coffee_mug,		None},
95645f5050Syouri{"cross",		XC_cross,		None},
96645f5050Syouri{"cross_reverse",	XC_cross_reverse,       None},
97645f5050Syouri{"crosshair",		XC_crosshair,		None},
98645f5050Syouri{"diamond_cross",	XC_diamond_cross,       None},
99645f5050Syouri{"dot",			XC_dot,			None},
100645f5050Syouri{"dotbox",		XC_dotbox,		None},
101645f5050Syouri{"double_arrow",	XC_double_arrow,	None},
102645f5050Syouri{"draft_large",		XC_draft_large,		None},
103645f5050Syouri{"draft_small",		XC_draft_small,		None},
104645f5050Syouri{"draped_box",		XC_draped_box,		None},
105645f5050Syouri{"exchange",		XC_exchange,		None},
106645f5050Syouri{"fleur",		XC_fleur,		None},
107645f5050Syouri{"gobbler",		XC_gobbler,		None},
108645f5050Syouri{"gumby",		XC_gumby,		None},
109645f5050Syouri{"hand1",		XC_hand1,		None},
110645f5050Syouri{"hand2",		XC_hand2,		None},
111645f5050Syouri{"heart",		XC_heart,		None},
112645f5050Syouri{"icon",		XC_icon,		None},
113645f5050Syouri{"iron_cross",		XC_iron_cross,		None},
114645f5050Syouri{"left_ptr",		XC_left_ptr,		None},
115645f5050Syouri{"left_side",		XC_left_side,		None},
116645f5050Syouri{"left_tee",		XC_left_tee,		None},
117645f5050Syouri{"leftbutton",		XC_leftbutton,		None},
118645f5050Syouri{"ll_angle",		XC_ll_angle,		None},
119645f5050Syouri{"lr_angle",		XC_lr_angle,		None},
120645f5050Syouri{"man",			XC_man,			None},
121645f5050Syouri{"middlebutton",	XC_middlebutton,	None},
122645f5050Syouri{"mouse",		XC_mouse,		None},
123645f5050Syouri{"pencil",		XC_pencil,		None},
124645f5050Syouri{"pirate",		XC_pirate,		None},
125645f5050Syouri{"plus",		XC_plus,		None},
126645f5050Syouri{"question_arrow",	XC_question_arrow,	None},
127645f5050Syouri{"right_ptr",		XC_right_ptr,		None},
128645f5050Syouri{"right_side",		XC_right_side,		None},
129645f5050Syouri{"right_tee",		XC_right_tee,		None},
130645f5050Syouri{"rightbutton",		XC_rightbutton,		None},
131645f5050Syouri{"rtl_logo",		XC_rtl_logo,		None},
132645f5050Syouri{"sailboat",		XC_sailboat,		None},
133645f5050Syouri{"sb_down_arrow",	XC_sb_down_arrow,       None},
134645f5050Syouri{"sb_h_double_arrow",	XC_sb_h_double_arrow,   None},
135645f5050Syouri{"sb_left_arrow",	XC_sb_left_arrow,       None},
136645f5050Syouri{"sb_right_arrow",	XC_sb_right_arrow,      None},
137645f5050Syouri{"sb_up_arrow",		XC_sb_up_arrow,		None},
138645f5050Syouri{"sb_v_double_arrow",	XC_sb_v_double_arrow,   None},
139645f5050Syouri{"shuttle",		XC_shuttle,		None},
140645f5050Syouri{"sizing",		XC_sizing,		None},
141645f5050Syouri{"spider",		XC_spider,		None},
142645f5050Syouri{"spraycan",		XC_spraycan,		None},
143645f5050Syouri{"star",		XC_star,		None},
144645f5050Syouri{"target",		XC_target,		None},
145645f5050Syouri{"tcross",		XC_tcross,		None},
146645f5050Syouri{"top_left_arrow",	XC_top_left_arrow,      None},
147645f5050Syouri{"top_left_corner",	XC_top_left_corner,	None},
148645f5050Syouri{"top_right_corner",	XC_top_right_corner,    None},
149645f5050Syouri{"top_side",		XC_top_side,		None},
150645f5050Syouri{"top_tee",		XC_top_tee,		None},
151645f5050Syouri{"trek",		XC_trek,		None},
152645f5050Syouri{"ul_angle",		XC_ul_angle,		None},
153645f5050Syouri{"umbrella",		XC_umbrella,		None},
154645f5050Syouri{"ur_angle",		XC_ur_angle,		None},
155645f5050Syouri{"watch",		XC_watch,		None},
156645f5050Syouri{"xterm",		XC_xterm,		None},
157645f5050Syouri};
158645f5050Syouri
159645f5050Syourivoid NewFontCursor (Cursor *cp, char *str)
160645f5050Syouri{
161645f5050Syouri    int i;
162645f5050Syouri
163645f5050Syouri    for (i = 0; i < sizeof(cursor_names)/sizeof(struct _CursorName); i++)
164645f5050Syouri    {
165645f5050Syouri	if (strcmp(str, cursor_names[i].name) == 0)
166645f5050Syouri	{
167645f5050Syouri	    if (cursor_names[i].cursor == None)
168645f5050Syouri		cursor_names[i].cursor = XCreateFontCursor(dpy,
169645f5050Syouri			cursor_names[i].shape);
170645f5050Syouri	    *cp = cursor_names[i].cursor;
171645f5050Syouri	    return;
172645f5050Syouri	}
173645f5050Syouri    }
174645f5050Syouri    fprintf (stderr, "%s:  unable to find font cursor \"%s\"\n",
175645f5050Syouri	     ProgramName, str);
176645f5050Syouri}
177645f5050Syouri
178645f5050Syouriint NewBitmapCursor(Cursor *cp, char *source, char *mask)
179645f5050Syouri{
180645f5050Syouri    XColor fore, back;
181645f5050Syouri    int hotx, hoty;
182645f5050Syouri    int sx, sy, mx, my;
183645f5050Syouri    unsigned int sw, sh, mw, mh;
184645f5050Syouri    Pixmap spm, mpm;
185645f5050Syouri    Colormap cmap = Scr->RootColormaps.cwins[0]->colormap->c;
186645f5050Syouri
187645f5050Syouri    fore.pixel = Scr->Black;
188645f5050Syouri    XQueryColor(dpy, cmap, &fore);
189645f5050Syouri    back.pixel = Scr->White;
190645f5050Syouri    XQueryColor(dpy, cmap, &back);
191645f5050Syouri
192645f5050Syouri    spm = GetBitmap(source);
193645f5050Syouri    if ((hotx = HotX) < 0) hotx = 0;
194645f5050Syouri    if ((hoty = HotY) < 0) hoty = 0;
195645f5050Syouri    mpm = GetBitmap(mask);
196645f5050Syouri
197645f5050Syouri    /* make sure they are the same size */
198645f5050Syouri
199645f5050Syouri    XGetGeometry(dpy, spm, &JunkRoot, &sx, &sy, &sw, &sh, &JunkBW,&JunkDepth);
200645f5050Syouri    XGetGeometry(dpy, mpm, &JunkRoot, &mx, &my, &mw, &mh, &JunkBW,&JunkDepth);
201645f5050Syouri    if (sw != mw || sh != mh)
202645f5050Syouri    {
203645f5050Syouri	fprintf (stderr,
204645f5050Syouri		 "%s:  cursor bitmaps \"%s\" and \"%s\" not the same size\n",
205645f5050Syouri		 ProgramName, source, mask);
206645f5050Syouri	return (1);
207645f5050Syouri    }
208645f5050Syouri    *cp = XCreatePixmapCursor(dpy, spm, mpm, &fore, &back, hotx,hoty);
209645f5050Syouri    return (0);
210645f5050Syouri}
211645f5050Syouri
212645f5050SyouriCursor MakeStringCursor (char *string)
213645f5050Syouri{
214645f5050Syouri    Cursor	cursor;
215645f5050Syouri    XColor	black, white;
216645f5050Syouri    Pixmap	bitmap;
217645f5050Syouri    unsigned int width, height, middle;
218645f5050Syouri    GC		gc;
219645f5050Syouri    Colormap	cmap = Scr->RootColormaps.cwins[0]->colormap->c;
220645f5050Syouri    MyFont	myfont = Scr->TitleBarFont;
221645f5050Syouri    XRectangle inc_rect;
222645f5050Syouri    XRectangle logical_rect;
223645f5050Syouri
224645f5050Syouri    black.pixel = Scr->Black;
225645f5050Syouri    XQueryColor (dpy, cmap, &black);
226645f5050Syouri    white.pixel = Scr->White;
227645f5050Syouri    XQueryColor (dpy, cmap, &white);
228645f5050Syouri
229645f5050Syouri    XmbTextExtents (myfont.font_set, string, strlen (string),
230645f5050Syouri		       &inc_rect, &logical_rect);
231645f5050Syouri    width  = logical_rect.width  + 4;
232645f5050Syouri    height = logical_rect.height + 2;
233645f5050Syouri    middle = myfont.ascent;
234645f5050Syouri    /*XQueryBestCursor (dpy, Scr->Root, width, height, &rwidth, &rheight);*/
235645f5050Syouri
236645f5050Syouri    bitmap = XCreatePixmap (dpy, Scr->Root, width, height, 1);
237645f5050Syouri    gc     = XCreateGC (dpy, bitmap, 0L, NULL);
238645f5050Syouri
239645f5050Syouri    XSetForeground (dpy, gc, 0L);
240645f5050Syouri    XFillRectangle (dpy, bitmap, gc, 0, 0, width, height);
241645f5050Syouri    XSetForeground (dpy, gc, 1L);
242645f5050Syouri    XDrawRectangle (dpy, bitmap, gc, 0, 0, width - 1, height - 1);
243645f5050Syouri
244645f5050Syouri    XmbDrawString (dpy, bitmap, myfont.font_set,
245645f5050Syouri		     gc, 2, middle, string, strlen (string));
246645f5050Syouri
247645f5050Syouri    cursor = XCreatePixmapCursor (dpy, bitmap, None, &black, &white, 0, 0);
248645f5050Syouri    XFreePixmap (dpy, bitmap);
249645f5050Syouri    XFreeGC (dpy, gc);
250645f5050Syouri    return (cursor);
251645f5050Syouri}
252