Change MacOS X mouse cursor Image - macos

Is there any way to change Mac OSX mouse cursor image as I am recording a video in which I want to use a different image for Mac mouse cursor and I tried many software that only change size of the mouse cursor, but not the image. So how can I replace default MacOSX mouse cursor image on the screen.

You can override -(void) resetCursorRects for a NSView subclass. Something like this:
-(void) resetCursorRects {
[super resetCursorRects];
// define cursor image and cursorRects
NSRect move = NSMakeRect((reg1UserStart + REGION_RESIZE_SIZE),REGION_Y_LOCATION, (self.reg1UserLength - (REGION_RESIZE_SIZE*2)), REGION_HEIGHT);
NSRect resizeStart = NSMakeRect(self.reg1UserStart, REGION_Y_LOCATION, REGION_RESIZE_SIZE, REGION_HEIGHT);
NSRect resizeEnd = NSMakeRect(((self.reg1UserStart + self.reg1UserLength) - REGION_RESIZE_SIZE), REGION_Y_LOCATION, REGION_RESIZE_SIZE, REGION_HEIGHT);
[self addCursorRect:move cursor:[NSCursor openHandCursor]];
[self addCursorRect:resizeStart cursor: [NSCursor resizeRightCursor]];
[self addCursorRect:resizeEnd cursor:[NSCursor resizeLeftCursor]];
}
You have to define a NSRect that represents the area that changes the mouse cursor, and then you give it an NSCursor type. This can be used to make custom cursors as well.
GW

Related

How to click through a window's transparent part in Cocoa and SFML

I use window.getSystemHandle() in SFML and convert it to NSWindow *, the following code is used to make the window transparent:
NSWindow *cocoa_window = (NSWindow*)window.getSystemHandle() ;
GLint opaque = 0;
[cocoa_window setStyleMask:NSWindowStyleMaskBorderless];
[cocoa_window setLevel: NSFloatingWindowLevel];
[[[cocoa_window contentView] openGLContext] setValues:&opaque forParameter:NSOpenGLContextParameterSurfaceOpacity];
[cocoa_window setBackgroundColor:[NSColor clearColor]];
[cocoa_window setOpaque:NO];
And I clean the window by:
window.clear(sf::Color::Transparent);
However, I can't click through the transparent part ( I want to receive mouse events when the user click on the opaque part) of the window. What can I do?
Should I replace the line [[[cocoa_window contentView] openGLContext] ...; with something else? Or do something on SFML part instead?

Why does a NSView keeps on receiving touch events after the cursor has left its frame?

I have an NSView with [self setAcceptsTouchEvents:YES];
After the cursor has left the NSView frame, this methods keeps on being called, until I click/begin a new gesture, I don't really understand when it stops.
-(void)touchesMovedWithEvent:(NSEvent *)theEvent {
[self doStuffs:theEvent];
}
with coordinates expressed in other windows coordinate systems.
Is there a way do prevent this, or convert the coordinates back the view coordinates system ?
NSResponder's touchesMovedWithEvent is called for touch events that start in your view, until they end (no matter if that's in your own view or elsewhere).
The event location is expressed in window coordinates. Converting to your view's coordinate system is easy:
- (void)touchesMovedWithEvent:(NSEvent *)event
{
NSPoint locInWindow;
NSPoint locInView;
locInWindow = [event locationInWindow];
locInView = [self convertPoint:locInWindow fromView:nil];
NSLog(#"Location in window: %#", NSStringFromPoint(locInWindow));
NSLog(#"Location in view: %#", NSStringFromPoint(locInView));
}
If you want to handle raw touches for your own multitouch gestures, this is likely not enough info. You'll want to use [event touchesMatchingPhase:NSTouchPhaseMoved inView:self], normalizedPosition, deviceSize etc. (see Apple's documentation).

Automatically resize an NSButton to fit programmatically changed text (Xcode)

I have an NSButton (Push Button) with some temporary title text built in Interface Builder / Xcode. Elsewhere, the title text inside the button is changed programmatically to a string of unknown length (actually, many times to many different lengths).
I'd like the button to automatically be resized (with a fixed right position--so it grows out to the left) to fit whatever length of string is programmatically inserted as button text. But I can't figure it out. Any suggestions? Thanks in advance!
If you can't use Auto Layout as suggested by #jtbandes (it's only available in Lion), then you can call [button sizeToFit] after setting its string value, which will make the button resize to fit its string. You would then need to adjust its frame based on the new width.
You can't do this automatically, but it would be easy to do in a subclass of NSButton.
#implementation RKSizeToFitButton
- (void)setStringValue:(NSString*)aString
{
//get the current frame
NSRect frame = [self frame];
//button label
[super setStringValue:aString];
//resize to fit the new string
[self sizeToFit];
//calculate the difference between the two frame widths
NSSize newSize = self.frame.size;
CGFloat widthDelta = newSize.width - NSWidth(frame);
//set the frame origin
[self setFrameOrigin:NSMakePoint(NSMinX(self.frame) - widthDelta, NSMinY(self.frame))];
}
#end
This way you can just set your button's class to RKSizeToFitButton in Interface Builder and then calling setStringValue: on the button to change its label will "just work" with no additional code.
Sure! Just use Auto Layout! :)

How to implement zoom/scale in a Cocoa AppKit-application

How do you implement zoom/scale in a Cocoa AppKit-application (i.e. not maximizing the window but scaling the window and all its subviews)? I think it's called zoomScale in iOS. Can it be done using Core Animations or Quartz 2D (e.g. CGContextScaleCTM) or am I forced to implement it manually in all my NSViews, NSCells, etc?
Each NSView has a bounds and frame, the frame is the rectangle that describes a view's placement within its superview's bounds. Most views have a bounds with a zero origin, and a size that matches their frame size, but this doesn't have to be the case. You can change the relationship of a view's bounds and frame to scale and translate both custom drawing and subviews. When you change the bounds of a view, it also affects the drawing of descendant views recursively.
The most straightforward way to change the bounds of a view is with -[NSView scaleUnitSquareToSize:]. In one of your views, try calling [self scaleUnitSquareToSize:NSMakeSize(2.0, 2.0)], and you should see the size of everything inside of it appear to be double.
Here's an example. Create a XIB file with a window containing a custom view, and a button. Set the custom view's custom class to MyView. Connect the button's action to the view's doubleSize: action. Build and run and hit the button. The red square in the custom view should double in size with each press.
/// MyView.h
#interface MyView : NSView {
}
- (IBAction)doubleSize:(id)sender;
#end
/// MyView.m
#implementation MyView
- (IBAction)doubleSize:(id)sender {
[self scaleUnitSquareToSize:NSMakeSize(2.0, 2.0)];
/// Important, changing the scale doesn't invalidate the display
[self setNeedsDisplay:YES];
}
- (void)drawRect:(NSRect)dirtyRect {
NSSize squareSize = NSMakeSize(8, 8);
NSRect square = NSMakeRect([self bounds].size.width / 2 - squareSize.width / 2,
[self bounds].size.height / 2 - squareSize.height / 2,
squareSize.width,
squareSize.height);
[[NSColor redColor] set];
NSRectFill(square);
}
#end

How do I get the inner/client size of a NSView subclass?

I am doing manual layouting for my Cocoa application and at some point I need to figure out what the inner size of a NSView subclass is. (E.g. What is the height available for my child view inside of a NSBox?)
One of the reasons is that I am using a coordinate system with origin at the top-left and need to perform coordinate transformations.
I could not figure out a way to get this size so far and would be glad if somebody can give me a hint.
Another very interesting property I would like to know is the minimum size of a view.
-bounds is the one you're looking for in most views. NSBox is a bit of a special case, however, since you want to look at the bounds of the box's content view, not the bounds of the box view itself (the box view includes the title, edges, etc.). Also, the bounds rect is always the real size of the box, while the frame rect can be modified relative to the bounds to apply transformations to the view's contents (such as squashing a 200x200 image into a 200x100 frame).
So, for most views you just use [parentView bounds], and for NSBox you'll use [[theBox contentView] bounds], and you'll use [[theBox contentView] addSubview: myView] rather than [parentView addSubview: myView] to add your content.
Unfortunately, there is no standard way to do this for all NSView subclasses. In your specific example, the position and size of a child view within an NSBox can be computed as follows:
NSRect availableRect = [someNSBox bounds];
NSSize boxMargins = [someBox contentViewMargins];
availableRect = NSInsetRect(availableRect, boxMargins.width, boxMargins.height);
If you find yourself using this often, you could create a category on NSBox as follows:
// MyNSBoxCategories.h
#interface NSBox (MyCategories)
- (NSRect)contentFrame;
#end
// MyNSBoxCategories.m
#implementation NSBox (MyCategories)
- (NSRect)contentFrame
{
NSRect frameRect = [self bounds];
NSSize margins = [self contentViewMargins];
return NSInsetRect(frameRect, margins.width, margins.height);
}
#end
And you would use it like so:
#import "MyNSBoxCategories.h"
//...
NSRect frameRect = [someNSBox contentFrame];
[myContentView setFrame:frameRect];
[someNSBox addSubview:myContentView];
The bounds property of NSView returns an NSRect with the origin (usually (0,0)) and the size of an NSView. See this Apple Developer documentation page.
I'm not sure (I never had to go too deep in that stuff), but isn't it [NSView bounds]?
http://www.cocoadev.com/index.pl?DifferenceBetweenFrameAndBounds

Resources