cursor.c revision 0bbfda8a
1/* 2 * Copyright 1989 Massachusetts Institute of Technology 3 * Copyright 1992 Claude Lecommandeur. 4 */ 5 6/*********************************************************************** 7 * 8 * $XConsortium: cursor.c,v 1.10 89/12/14 14:52:23 jim Exp $ 9 * 10 * cursor creation code 11 * 12 * 05-Apr-89 Thomas E. LaStrange File created 13 * 14 * Do the necessary modification to be integrated in ctwm. 15 * Can no longer be used for the standard twm. 16 * 17 * 22-April-92 Claude Lecommandeur. 18 * 19 * 20 ***********************************************************************/ 21 22#include "ctwm.h" 23 24#include <stdio.h> 25 26#include <X11/cursorfont.h> 27 28#include "screen.h" 29#include "cursor.h" 30#include "image_bitmap.h" 31 32static struct _CursorName { 33 const char *name; 34 unsigned int shape; 35 Cursor cursor; 36} cursor_names[] = { 37 38 {"X_cursor", XC_X_cursor, None}, 39 {"arrow", XC_arrow, None}, 40 {"based_arrow_down", XC_based_arrow_down, None}, 41 {"based_arrow_up", XC_based_arrow_up, None}, 42 {"boat", XC_boat, None}, 43 {"bogosity", XC_bogosity, None}, 44 {"bottom_left_corner", XC_bottom_left_corner, None}, 45 {"bottom_right_corner", XC_bottom_right_corner, None}, 46 {"bottom_side", XC_bottom_side, None}, 47 {"bottom_tee", XC_bottom_tee, None}, 48 {"box_spiral", XC_box_spiral, None}, 49 {"center_ptr", XC_center_ptr, None}, 50 {"circle", XC_circle, None}, 51 {"clock", XC_clock, None}, 52 {"coffee_mug", XC_coffee_mug, None}, 53 {"cross", XC_cross, None}, 54 {"cross_reverse", XC_cross_reverse, None}, 55 {"crosshair", XC_crosshair, None}, 56 {"diamond_cross", XC_diamond_cross, None}, 57 {"dot", XC_dot, None}, 58 {"dotbox", XC_dotbox, None}, 59 {"double_arrow", XC_double_arrow, None}, 60 {"draft_large", XC_draft_large, None}, 61 {"draft_small", XC_draft_small, None}, 62 {"draped_box", XC_draped_box, None}, 63 {"exchange", XC_exchange, None}, 64 {"fleur", XC_fleur, None}, 65 {"gobbler", XC_gobbler, None}, 66 {"gumby", XC_gumby, None}, 67 {"hand1", XC_hand1, None}, 68 {"hand2", XC_hand2, None}, 69 {"heart", XC_heart, None}, 70 {"icon", XC_icon, None}, 71 {"iron_cross", XC_iron_cross, None}, 72 {"left_ptr", XC_left_ptr, None}, 73 {"left_side", XC_left_side, None}, 74 {"left_tee", XC_left_tee, None}, 75 {"leftbutton", XC_leftbutton, None}, 76 {"ll_angle", XC_ll_angle, None}, 77 {"lr_angle", XC_lr_angle, None}, 78 {"man", XC_man, None}, 79 {"middlebutton", XC_middlebutton, None}, 80 {"mouse", XC_mouse, None}, 81 {"pencil", XC_pencil, None}, 82 {"pirate", XC_pirate, None}, 83 {"plus", XC_plus, None}, 84 {"question_arrow", XC_question_arrow, None}, 85 {"right_ptr", XC_right_ptr, None}, 86 {"right_side", XC_right_side, None}, 87 {"right_tee", XC_right_tee, None}, 88 {"rightbutton", XC_rightbutton, None}, 89 {"rtl_logo", XC_rtl_logo, None}, 90 {"sailboat", XC_sailboat, None}, 91 {"sb_down_arrow", XC_sb_down_arrow, None}, 92 {"sb_h_double_arrow", XC_sb_h_double_arrow, None}, 93 {"sb_left_arrow", XC_sb_left_arrow, None}, 94 {"sb_right_arrow", XC_sb_right_arrow, None}, 95 {"sb_up_arrow", XC_sb_up_arrow, None}, 96 {"sb_v_double_arrow", XC_sb_v_double_arrow, None}, 97 {"shuttle", XC_shuttle, None}, 98 {"sizing", XC_sizing, None}, 99 {"spider", XC_spider, None}, 100 {"spraycan", XC_spraycan, None}, 101 {"star", XC_star, None}, 102 {"target", XC_target, None}, 103 {"tcross", XC_tcross, None}, 104 {"top_left_arrow", XC_top_left_arrow, None}, 105 {"top_left_corner", XC_top_left_corner, None}, 106 {"top_right_corner", XC_top_right_corner, None}, 107 {"top_side", XC_top_side, None}, 108 {"top_tee", XC_top_tee, None}, 109 {"trek", XC_trek, None}, 110 {"ul_angle", XC_ul_angle, None}, 111 {"umbrella", XC_umbrella, None}, 112 {"ur_angle", XC_ur_angle, None}, 113 {"watch", XC_watch, None}, 114 {"xterm", XC_xterm, None}, 115}; 116 117void NewFontCursor(Cursor *cp, const char *str) 118{ 119 int i; 120 121 for(i = 0; i < sizeof(cursor_names) / sizeof(struct _CursorName); i++) { 122 if(strcmp(str, cursor_names[i].name) == 0) { 123 if(cursor_names[i].cursor == None) 124 cursor_names[i].cursor = XCreateFontCursor(dpy, 125 cursor_names[i].shape); 126 *cp = cursor_names[i].cursor; 127 return; 128 } 129 } 130 fprintf(stderr, "%s: unable to find font cursor \"%s\"\n", 131 ProgramName, str); 132} 133 134int NewBitmapCursor(Cursor *cp, char *source, char *mask) 135{ 136 XColor fore, back; 137 int hotx, hoty; 138 int sx, sy, mx, my; 139 unsigned int sw, sh, mw, mh; 140 Pixmap spm, mpm; 141 Colormap cmap = Scr->RootColormaps.cwins[0]->colormap->c; 142 143 fore.pixel = Scr->Black; 144 XQueryColor(dpy, cmap, &fore); 145 back.pixel = Scr->White; 146 XQueryColor(dpy, cmap, &back); 147 148 spm = GetBitmap(source); 149 if((hotx = HotX) < 0) { 150 hotx = 0; 151 } 152 if((hoty = HotY) < 0) { 153 hoty = 0; 154 } 155 mpm = GetBitmap(mask); 156 157 /* make sure they are the same size */ 158 159 XGetGeometry(dpy, spm, &JunkRoot, &sx, &sy, &sw, &sh, &JunkBW, &JunkDepth); 160 XGetGeometry(dpy, mpm, &JunkRoot, &mx, &my, &mw, &mh, &JunkBW, &JunkDepth); 161 if(sw != mw || sh != mh) { 162 fprintf(stderr, 163 "%s: cursor bitmaps \"%s\" and \"%s\" not the same size\n", 164 ProgramName, source, mask); 165 return (1); 166 } 167 *cp = XCreatePixmapCursor(dpy, spm, mpm, &fore, &back, hotx, hoty); 168 return (0); 169} 170 171Cursor MakeStringCursor(char *string) 172{ 173 Cursor cursor; 174 XColor black, white; 175 Pixmap bitmap; 176 unsigned int width, height, middle; 177 GC gc; 178 Colormap cmap = Scr->RootColormaps.cwins[0]->colormap->c; 179 MyFont myfont = Scr->TitleBarFont; 180 XRectangle inc_rect; 181 XRectangle logical_rect; 182 183 black.pixel = Scr->Black; 184 XQueryColor(dpy, cmap, &black); 185 white.pixel = Scr->White; 186 XQueryColor(dpy, cmap, &white); 187 188 XmbTextExtents(myfont.font_set, string, strlen(string), 189 &inc_rect, &logical_rect); 190 width = logical_rect.width + 4; 191 height = logical_rect.height + 2; 192 middle = myfont.ascent; 193 /*XQueryBestCursor (dpy, Scr->Root, width, height, &rwidth, &rheight);*/ 194 195 bitmap = XCreatePixmap(dpy, Scr->Root, width, height, 1); 196 gc = XCreateGC(dpy, bitmap, 0L, NULL); 197 198 XSetForeground(dpy, gc, 0L); 199 XFillRectangle(dpy, bitmap, gc, 0, 0, width, height); 200 XSetForeground(dpy, gc, 1L); 201 XDrawRectangle(dpy, bitmap, gc, 0, 0, width - 1, height - 1); 202 203 XmbDrawString(dpy, bitmap, myfont.font_set, 204 gc, 2, middle, string, strlen(string)); 205 206 cursor = XCreatePixmapCursor(dpy, bitmap, None, &black, &white, 0, 0); 207 XFreePixmap(dpy, bitmap); 208 XFreeGC(dpy, gc); 209 return (cursor); 210} 211