[Drawkit] Keeping a drawing tool selected
Graham Cox
graham.cox at bigpond.com
Wed Jul 9 18:05:48 PDT 2008
The tool controller is a subclass of DKViewController. There is one
per view (DKDrawingView) but the actual owner of the tool controller
is the drawing instance (DKDrawing). So you can get to the controller
from the "back end" using DKDrawing's -controllers method, or from the
"front end" by using the view's -controller method. The drawing has a
to-many relationship with the controllers, the view has a to-one
relationship.
The reason that simply treating First Responder as if it were the
controller works is because of the view's automatic invocation
forwarding, which initially punts messages it doesn't respond to to
the controller. Of course your view does have to *be* 1stR otherwise
the message will just end up as vapour ;-)
This architecture is very similar to the document architecture in
Cocoa, where a document owns one or more window controllers, and in
turn they own a single window each. One difference is that the
controller doesn't own the view - it's a weak ref - since the view
already has an owner and we don't want to set up a retain cycle in the
case that the view automatically creates a drawing.
This is more than you wanted to know, but hopefully helps in a general
way.
cheers, Graham
On 10 Jul 2008, at 5:13 am, Brad Larson wrote:
> That does the trick. It seemed like it should be that simple.
>
> Out of curiosity, where does the current instance of
> DKToolController reside? I'm currently sending the message to the
> first responder in the following drawing tool selection code
>
> [(DKToolController *)[[NSApp mainWindow] firstResponder]
> setAutomaticallyRevertsToSelectionTool:NO];
>
> DKDrawablePath *line = [[DKDrawablePath alloc] init];
> [line setPathEditingMode:kGCPathCreateModeLineCreate];
> [line setStyle:[[self drawing]
> metadataObjectForKey:@"emptyFeatureStyle"]];
> DKDrawingTool *tool = [[DKObjectCreationTool alloc]
> initWithPrototypeObject:line];
> [(DKToolController *)[[NSApp mainWindow] firstResponder]
> setDrawingTool:tool]; // I could also use [tool set] here, which
> sends the same message
>
> and getting some instance of DKToolController to respond, but I
> don't know where this instance exists. Again, this works just fine
> now, I'm just trying to understand the inner workings.
>
> On Jul 8, 2008, at 8:29 PM, Graham Cox wrote:
>
>> This is a property of the tool controller. Call:
>>
>> [theToolController setAutomaticallyRevertsToSelectionTool:NO];
>>
More information about the Drawkit
mailing list