1996-05-30 20:03:15 +00:00
|
|
|
/*
|
|
|
|
NSCursor.m
|
|
|
|
|
1997-02-12 22:21:43 +00:00
|
|
|
Holds an image to use as a cursor
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
Copyright (C) 1996,1999 Free Software Foundation, Inc.
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
Author: Scott Christley <scottc@net-community.com>
|
|
|
|
Date: 1996
|
|
|
|
|
|
|
|
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
|
1996-10-18 17:14:13 +00:00
|
|
|
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.
|
1996-05-30 20:03:15 +00:00
|
|
|
*/
|
|
|
|
|
1997-02-18 18:45:16 +00:00
|
|
|
#include <Foundation/NSArray.h>
|
1997-02-18 00:29:25 +00:00
|
|
|
#include <AppKit/NSCursor.h>
|
2000-02-25 17:09:49 +00:00
|
|
|
#include <AppKit/NSImage.h>
|
|
|
|
#include <AppKit/NSBitmapImageRep.h>
|
1999-10-29 18:43:48 +00:00
|
|
|
#include <AppKit/NSGraphicsContext.h>
|
|
|
|
#include <AppKit/DPSOperators.h>
|
1997-02-12 22:21:43 +00:00
|
|
|
|
|
|
|
// Class variables
|
1997-02-18 18:45:16 +00:00
|
|
|
static NSMutableArray *gnustep_gui_cursor_stack;
|
1997-02-12 22:21:43 +00:00
|
|
|
static NSCursor *gnustep_gui_current_cursor;
|
|
|
|
static BOOL gnustep_gui_hidden_until_move;
|
2001-07-23 22:58:54 +00:00
|
|
|
static Class NSCursor_class;
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2001-12-03 21:17:12 +00:00
|
|
|
static NSCursor *arrowCursor = nil;
|
|
|
|
static NSCursor *ibeamCursor = nil;
|
|
|
|
|
1996-05-30 20:03:15 +00:00
|
|
|
@implementation NSCursor
|
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
/*
|
|
|
|
* Class methods
|
|
|
|
*/
|
|
|
|
+ (void) initialize
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
|
|
|
if (self == [NSCursor class])
|
|
|
|
{
|
|
|
|
// Initial version
|
|
|
|
[self setVersion:1];
|
1997-02-12 22:21:43 +00:00
|
|
|
|
|
|
|
// Initialize class variables
|
2001-07-23 22:58:54 +00:00
|
|
|
NSCursor_class = self;
|
1997-02-18 18:45:16 +00:00
|
|
|
gnustep_gui_cursor_stack = [[NSMutableArray alloc] initWithCapacity: 2];
|
1997-02-12 22:21:43 +00:00
|
|
|
gnustep_gui_hidden_until_move = YES;
|
1999-11-15 16:00:47 +00:00
|
|
|
[[self arrowCursor] push];
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
- (void *) _cid
|
1999-10-29 18:43:48 +00:00
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
return _cid;
|
1999-10-29 18:43:48 +00:00
|
|
|
}
|
|
|
|
|
2001-05-04 03:19:32 +00:00
|
|
|
- (void) _setCid: (void *)val
|
1999-10-29 18:43:48 +00:00
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
_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;
|
1999-10-29 18:43:48 +00:00
|
|
|
}
|
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
/*
|
|
|
|
* Setting the Cursor
|
|
|
|
*/
|
|
|
|
+ (void) hide
|
1997-02-12 22:21:43 +00:00
|
|
|
{
|
1999-10-29 18:43:48 +00:00
|
|
|
DPShidecursor(GSCurrentContext());
|
1997-02-12 22:21:43 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
+ (void) pop
|
1997-02-12 22:21:43 +00:00
|
|
|
{
|
1999-11-15 16:00:47 +00:00
|
|
|
/*
|
|
|
|
* 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];
|
|
|
|
|
2001-07-23 22:58:54 +00:00
|
|
|
[gnustep_gui_current_cursor set];
|
1999-11-15 16:00:47 +00:00
|
|
|
}
|
1997-02-12 22:21:43 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
+ (void) setHiddenUntilMouseMoves: (BOOL)flag
|
1997-02-12 22:21:43 +00:00
|
|
|
{
|
|
|
|
gnustep_gui_hidden_until_move = flag;
|
|
|
|
}
|
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
+ (BOOL) isHiddenUntilMouseMoves
|
1997-02-12 22:21:43 +00:00
|
|
|
{
|
|
|
|
return gnustep_gui_hidden_until_move;
|
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
+ (void) unhide
|
1997-02-12 22:21:43 +00:00
|
|
|
{
|
1999-10-29 18:43:48 +00:00
|
|
|
DPSshowcursor(GSCurrentContext());
|
1997-02-12 22:21:43 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
/*
|
|
|
|
* Getting the Cursor
|
|
|
|
*/
|
|
|
|
+ (NSCursor*) arrowCursor
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2001-12-03 21:17:12 +00:00
|
|
|
if (arrowCursor == nil)
|
|
|
|
{
|
|
|
|
void *c;
|
2001-07-23 22:58:54 +00:00
|
|
|
|
2001-12-03 21:17:12 +00:00
|
|
|
arrowCursor = [[NSCursor_class alloc] initWithImage: nil];
|
|
|
|
DPSstandardcursor(GSCurrentContext(), GSArrowCursor, &c);
|
|
|
|
[arrowCursor _setCid: c];
|
|
|
|
}
|
|
|
|
return arrowCursor;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
+ (NSCursor*) currentCursor
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
1997-02-12 22:21:43 +00:00
|
|
|
return gnustep_gui_current_cursor;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
+ (NSCursor*) IBeamCursor
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2001-12-03 21:17:12 +00:00
|
|
|
if (ibeamCursor == nil)
|
|
|
|
{
|
|
|
|
void *c;
|
2001-07-23 22:58:54 +00:00
|
|
|
|
2001-12-03 21:17:12 +00:00
|
|
|
ibeamCursor = [[NSCursor_class alloc] initWithImage: nil];
|
|
|
|
DPSstandardcursor(GSCurrentContext(), GSIBeamCursor, &c);
|
|
|
|
[ibeamCursor _setCid: c];
|
|
|
|
}
|
|
|
|
return ibeamCursor;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
/*
|
|
|
|
* Initializing a New NSCursor Object
|
|
|
|
*/
|
|
|
|
- (id) init
|
1997-02-12 22:21:43 +00:00
|
|
|
{
|
1999-11-15 16:00:47 +00:00
|
|
|
return [self initWithImage: nil hotSpot: NSMakePoint(0,15)];
|
1997-02-12 22:21:43 +00:00
|
|
|
}
|
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
- (id) initWithImage: (NSImage *)newImage
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
1999-11-15 16:00:47 +00:00
|
|
|
return [self initWithImage: newImage
|
|
|
|
hotSpot: NSMakePoint(0,15)];
|
|
|
|
}
|
1997-02-12 22:21:43 +00:00
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
- (id) initWithImage: (NSImage *)newImage hotSpot: (NSPoint)spot
|
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
//_is_set_on_mouse_entered = NO;
|
|
|
|
//_is_set_on_mouse_exited = NO;
|
|
|
|
_hot_spot = spot;
|
|
|
|
[self setImage: newImage];
|
|
|
|
|
1997-02-12 22:21:43 +00:00
|
|
|
return self;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:58:54 +00:00
|
|
|
- (id)initWithImage:(NSImage *)newImage
|
|
|
|
foregroundColorHint:(NSColor *)fg
|
|
|
|
backgroundColorHint:(NSColor *)bg
|
|
|
|
hotSpot:(NSPoint)hotSpot
|
|
|
|
{
|
2001-12-03 21:17:12 +00:00
|
|
|
// FIXME: fg and bg should be set
|
2001-07-23 22:58:54 +00:00
|
|
|
return [self initWithImage: newImage hotSpot: hotSpot];
|
|
|
|
}
|
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
/*
|
|
|
|
* Defining the Cursor
|
|
|
|
*/
|
|
|
|
- (NSPoint) hotSpot
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
// FIXME: This wont work for the two standard cursor
|
|
|
|
return _hot_spot;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
- (NSImage*) image
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
// FIXME: This wont work for the two standard cursor
|
|
|
|
return _cursor_image;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
- (void) setHotSpot: (NSPoint)spot
|
1997-02-12 22:21:43 +00:00
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
_hot_spot = spot;
|
|
|
|
[self _computeCid];
|
1997-02-12 22:21:43 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
- (void) setImage: (NSImage *)newImage
|
1997-02-12 22:21:43 +00:00
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
ASSIGN(_cursor_image, newImage);
|
|
|
|
[self _computeCid];
|
1997-02-12 22:21:43 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
/*
|
|
|
|
* Setting the Cursor
|
|
|
|
*/
|
|
|
|
- (BOOL) isSetOnMouseEntered
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
return _is_set_on_mouse_entered;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
- (BOOL) isSetOnMouseExited
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
return _is_set_on_mouse_exited;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
- (void) mouseEntered: (NSEvent*)theEvent
|
1997-02-12 22:21:43 +00:00
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
if (_is_set_on_mouse_entered == YES)
|
1999-11-15 16:00:47 +00:00
|
|
|
{
|
|
|
|
[self set];
|
|
|
|
}
|
2001-07-23 22:58:54 +00:00
|
|
|
else if (_is_set_on_mouse_exited == NO)
|
1999-11-15 16:00:47 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Undocumented behavior - if a cursor is not set on exit or entry,
|
|
|
|
* we assume a push-pop situation instead.
|
|
|
|
*/
|
|
|
|
[self push];
|
|
|
|
}
|
1997-02-12 22:21:43 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
- (void) mouseExited: (NSEvent*)theEvent
|
1997-02-12 22:21:43 +00:00
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
if (_is_set_on_mouse_exited == YES)
|
1999-11-15 16:00:47 +00:00
|
|
|
{
|
|
|
|
[self set];
|
|
|
|
}
|
2001-07-23 22:58:54 +00:00
|
|
|
else if (_is_set_on_mouse_entered == NO)
|
1999-11-15 16:00:47 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Undocumented behavior - if a cursor is not set on exit or entry,
|
|
|
|
* we assume a push-pop situation instead.
|
|
|
|
*/
|
|
|
|
[self pop];
|
|
|
|
}
|
1997-02-12 22:21:43 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
- (void) pop
|
1997-02-12 22:21:43 +00:00
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
[NSCursor_class pop];
|
1997-02-12 22:21:43 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
- (void) push
|
1997-02-12 22:21:43 +00:00
|
|
|
{
|
1997-02-18 18:45:16 +00:00
|
|
|
[gnustep_gui_cursor_stack addObject: self];
|
2001-07-23 22:58:54 +00:00
|
|
|
[self set];
|
1997-02-12 22:21:43 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
- (void) set
|
1997-02-12 22:21:43 +00:00
|
|
|
{
|
|
|
|
gnustep_gui_current_cursor = self;
|
2001-07-23 22:58:54 +00:00
|
|
|
if (_cid)
|
1999-11-15 16:00:47 +00:00
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
DPSsetcursorcolor(GSCurrentContext(), -1, 0, 0, 1, 1, 1, _cid);
|
1999-11-15 16:00:47 +00:00
|
|
|
}
|
1997-02-12 22:21:43 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
- (void) setOnMouseEntered: (BOOL)flag
|
1997-02-12 22:21:43 +00:00
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
_is_set_on_mouse_entered = flag;
|
1997-02-12 22:21:43 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
- (void) setOnMouseExited: (BOOL)flag
|
1997-02-12 22:21:43 +00:00
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
_is_set_on_mouse_exited = flag;
|
1997-02-12 22:21:43 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1999-11-15 16:00:47 +00:00
|
|
|
/*
|
|
|
|
* NSCoding protocol
|
|
|
|
*/
|
1999-03-02 08:58:30 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
// 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];
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:58:30 +00:00
|
|
|
- (id) initWithCoder: (NSCoder*)aDecoder
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2001-07-23 22:58:54 +00:00
|
|
|
[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];
|
|
|
|
|
1996-05-30 20:03:15 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|