1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
/** <title>NSCursor</title>
<abstract>Holds an image to use as a cursor</abstract>
Copyright (C) 1996,1999,2001 Free Software Foundation, Inc.
Author: Scott Christley <scottc@net-community.com>
Date: 1996
Author: Adam Fedor <fedor@gnu.org>
Date: Dec 2001
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <Foundation/NSArray.h>
#include <Foundation/NSDebug.h>
#include <AppKit/NSColor.h>
#include <AppKit/NSCursor.h>
#include <AppKit/NSGraphics.h>
#include <AppKit/NSImage.h>
#include <AppKit/NSBitmapImageRep.h>
#include <AppKit/NSGraphicsContext.h>
#include <AppKit/DPSOperators.h>
// Class variables
static NSMutableArray *gnustep_gui_cursor_stack;
static NSCursor *gnustep_gui_current_cursor;
static BOOL gnustep_gui_hidden_until_move;
static Class NSCursor_class;
static NSMutableDictionary *cursorDict = nil;
@implementation NSCursor
/*
* Class methods
*/
+ (void) initialize
{
if (self == [NSCursor class])
{
// Initial version
[self setVersion:1];
// Initialize class variables
NSCursor_class = self;
gnustep_gui_cursor_stack = [[NSMutableArray alloc] initWithCapacity: 2];
gnustep_gui_hidden_until_move = YES;
cursorDict = RETAIN([NSMutableDictionary dictionary]);
[[self arrowCursor] push];
}
}
- (void *) _cid
{
return _cid;
}
- (void) _setCid: (void *)val
{
_cid = val;
}
- (void) _computeCid
{
void *c;
NSBitmapImageRep *rep;
if (_cursor_image == nil)
{
_cid = NULL;
return;
}
rep = (NSBitmapImageRep *)[_cursor_image bestRepresentationForDevice: nil];
/* FIXME: Handle cached image reps also */
if (!rep || ![rep respondsToSelector: @selector(samplesPerPixel)])
{
NSLog(@"NSCursor can only handle NSBitmapImageReps for now");
return;
}
if (_hot_spot.x >= [rep pixelsWide])
_hot_spot.x = [rep pixelsWide]-1;
if (_hot_spot.y >= [rep pixelsHigh])
_hot_spot.y = [rep pixelsHigh]-1;
DPSimagecursor(GSCurrentContext(), _hot_spot.x, _hot_spot.y,
[rep pixelsWide], [rep pixelsHigh],
[rep samplesPerPixel], [rep bitmapData], &c);
_cid = c;
}
/*
* Setting the Cursor
*/
+ (void) hide
{
DPShidecursor(GSCurrentContext());
}
+ (void) pop
{
/*
* The object we pop is the current cursor
*/
if ([gnustep_gui_cursor_stack count] > 1)
{
[gnustep_gui_cursor_stack removeLastObject];
gnustep_gui_current_cursor = [gnustep_gui_cursor_stack lastObject];
NSDebugLLog(@"NSCursor", @"Cursor pop");
[gnustep_gui_current_cursor set];
}
}
+ (void) setHiddenUntilMouseMoves: (BOOL)flag
{
gnustep_gui_hidden_until_move = flag;
}
+ (BOOL) isHiddenUntilMouseMoves
{
return gnustep_gui_hidden_until_move;
}
+ (void) unhide
{
DPSshowcursor(GSCurrentContext());
}
/*
* Getting the Cursor
*/
+ (NSCursor*) arrowCursor
{
NSString *name = @"GSArrowCursor";
NSCursor *cursor = [cursorDict objectForKey: name];
if (cursor == nil)
{
void *c;
cursor = [[NSCursor_class alloc] initWithImage: nil];
DPSstandardcursor(GSCurrentContext(), GSArrowCursor, &c);
[cursor _setCid: c];
[cursorDict setObject: cursor forKey: name];
RELEASE(cursor);
}
return cursor;
}
+ (NSCursor*) currentCursor
{
return gnustep_gui_current_cursor;
}
+ (NSCursor*) IBeamCursor
{
NSString *name = @"GSIBeamCursor";
NSCursor *cursor = [cursorDict objectForKey: name];
if (cursor == nil)
{
void *c;
cursor = [[NSCursor_class alloc] initWithImage: nil];
DPSstandardcursor(GSCurrentContext(), GSIBeamCursor, &c);
[cursor _setCid: c];
[cursorDict setObject: cursor forKey: name];
RELEASE(cursor);
}
return cursor;
}
+ (NSCursor*) greenArrowCursor
{
NSString *name = @"GSGreenArrowCursor";
NSCursor *cursor = [cursorDict objectForKey: name];
if (cursor == nil)
{
void *c;
cursor = [[NSCursor_class alloc] initWithImage: nil];
DPSstandardcursor(GSCurrentContext(), GSArrowCursor, &c);
DPSsetcursorcolor(GSCurrentContext (), 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, c);
[cursor _setCid: c];
[cursorDict setObject: cursor forKey: name];
RELEASE(cursor);
}
return cursor;
}
/*
* Initializing a New NSCursor Object
*/
- (id) init
{
return [self initWithImage: nil hotSpot: NSMakePoint(0,15)];
}
- (id) initWithImage: (NSImage *)newImage
{
return [self initWithImage: newImage
hotSpot: NSMakePoint(0,15)];
}
- (id) initWithImage: (NSImage *)newImage hotSpot: (NSPoint)hotSpot
{
//_is_set_on_mouse_entered = NO;
//_is_set_on_mouse_exited = NO;
_hot_spot = hotSpot;
[self setImage: newImage];
return self;
}
- (id)initWithImage:(NSImage *)newImage
foregroundColorHint:(NSColor *)fg
backgroundColorHint:(NSColor *)bg
hotSpot:(NSPoint)hotSpot
{
NSCursor *cursor = [self initWithImage: newImage hotSpot: hotSpot];
if (fg || bg)
{
if (bg == nil)
bg = [NSColor whiteColor];
if (fg == nil)
fg = [NSColor blackColor];
bg = [bg colorUsingColorSpaceName: NSDeviceRGBColorSpace];
fg = [fg colorUsingColorSpaceName: NSDeviceRGBColorSpace];
NSLog(@"fg color is %@", fg);
DPSsetcursorcolor(GSCurrentContext (),
[fg redComponent],
[fg greenComponent],
[fg blueComponent],
[bg redComponent],
[bg greenComponent],
[bg blueComponent],
_cid);
}
return cursor;
}
/*
* Defining the Cursor
*/
- (NSPoint) hotSpot
{
// FIXME: This wont work for the two standard cursor
return _hot_spot;
}
- (NSImage*) image
{
// FIXME: This wont work for the two standard cursor
return _cursor_image;
}
- (void) setHotSpot: (NSPoint)spot
{
_hot_spot = spot;
[self _computeCid];
}
- (void) setImage: (NSImage *)newImage
{
ASSIGN(_cursor_image, newImage);
[self _computeCid];
}
/*
* Setting the Cursor
*/
- (BOOL) isSetOnMouseEntered
{
return _is_set_on_mouse_entered;
}
- (BOOL) isSetOnMouseExited
{
return _is_set_on_mouse_exited;
}
- (void) mouseEntered: (NSEvent*)theEvent
{
if (_is_set_on_mouse_entered == YES)
{
[self set];
}
else if (_is_set_on_mouse_exited == NO)
{
/*
* Undocumented behavior - if a cursor is not set on exit or entry,
* we assume a push-pop situation instead.
*/
[self push];
}
}
- (void) mouseExited: (NSEvent*)theEvent
{
if (_is_set_on_mouse_exited == YES)
{
[self set];
}
else if (_is_set_on_mouse_entered == NO)
{
/*
* Undocumented behavior - if a cursor is not set on exit or entry,
* we assume a push-pop situation instead.
*/
[self pop];
}
}
- (void) pop
{
[NSCursor_class pop];
}
- (void) push
{
[gnustep_gui_cursor_stack addObject: self];
[self set];
NSDebugLLog(@"NSCursor", @"Cursor push %p", _cid);
}
- (void) set
{
gnustep_gui_current_cursor = self;
if (_cid)
{
DPSsetcursorcolor(GSCurrentContext(), -1, 0, 0, 1, 1, 1, _cid);
}
}
- (void) setOnMouseEntered: (BOOL)flag
{
_is_set_on_mouse_entered = flag;
}
- (void) setOnMouseExited: (BOOL)flag
{
_is_set_on_mouse_exited = flag;
}
/*
* NSCoding protocol
*/
- (void) encodeWithCoder: (NSCoder*)aCoder
{
// FIXME: This wont work for the two standard cursor
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &_is_set_on_mouse_entered];
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &_is_set_on_mouse_exited];
[aCoder encodeObject: _cursor_image];
[aCoder encodePoint: _hot_spot];
}
- (id) initWithCoder: (NSCoder*)aDecoder
{
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &_is_set_on_mouse_entered];
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &_is_set_on_mouse_exited];
_cursor_image = [aDecoder decodeObject];
_hot_spot = [aDecoder decodePoint];
[self _computeCid];
return self;
}
@end
|