Change the cursor for an entire view

Hello,

What is missing here? Of course my cursor does not change when entering “myView”:

set gPencilCursor to current application's NSCursor's alloc's initWithImage_hotSpot_(NSImage's imageNamed_("Pencil"),{0,0})
myView's addCursorRect_cursor_(myView's visibleRect(),gPencilCursor)

Thanks,

I think this is another one of those things that you need to do in a view that’s of your script’s class. So make your script’s parent NSView, set the class of the view you want to have the new cursor in (I did it with the window’s content view) to your script’s class and implement resetCursorRects like this:

script CursorsAppDelegate
	property parent : class "NSView"
	property gPencilCursor : missing value
	
	on resetCursorRects()
		set gPencilCursor to current application's NSCursor's alloc's initWithImage_hotSpot_(current application's NSImage's imageNamed_("Pencil"), {0, 0})
		addCursorRect_cursor_(visibleRect(), gPencilCursor) 
	end resetCursorRects
	
end script

Ric

Hi Ric,

Yes, “this is another one of those things” that I need to do :slight_smile:

  1. My Script Class is NSTextView, I suppose it’s OK as NSTextView is a descendant of NSView.
  2. The view I want to adjust the cursor is programmatically defined and added to a window. May I set this class to myScriptApp class by code?

Imagine a “tool palette” where the user selects a tool, and a view where the cursor, symbolizing the tool, has to be updated.

The solution is as follows:

First you define your cursor (in my case I stick to the old 16x16 pixel cursor, but you are not limited to this size). A .png picture is perfect for defining a cursor and the transparent parts of it.

You have to give the “hot spot” of the cursor, the pixel which is the “tip” of your cursor. The coordinates are starting from the bottom left (0,0) and increasing up and right to ( width-1, height-1).

Then you store your cursor (for example in applicationWillFinishLaunching_) into a property/global variable:

The tool palette has a handler, changeTool_, called when the palette is clicked (in my case it is a segmented control, bound to property named myToolPalette):

A click on the tool palette makes its window the key window. Note that resetCursorRects() is the NSWindow method, not the NSView method. This will reset all previously defined cursor rects for this window.

The addCursorRect_cursor_ method will then set the rectangle where the given cursor will appear. In my case, I choose the visibleRect, but I suppose the frame or the bounds will do (not tested).

All is done without setting your script to a given class (mine is always NSObject).

I hope this will be useful to somebody.

Regards,