Re: Moving controls interactively
Scott Stevenson <[email protected]>
| Newsgroups | gmane.comp.macosx.devel |
|---|---|
| Message-ID | <[email protected]> |
On Sep 26, 2009, at 5:22 AM, Andrea D'Amore wrote:
> I'd like to let an user move controls around the window, i.e. he
> should be able to both rearrange buttons, text fields, labels and
> images and to use them.
>
> I'm using a non NSDocument based project in XCode, I created a
> MyWindow subclass of NSWindow and I overrode sendEvent.
> In sendEvent I check if a flag in controller is set and then either
> pass along sendEvent by running [super sendEvent:event]; or I set
> the control position using setFrame.
Overriding sendEvent is NSWindow is almost always too low level – you
end up subverting the Cocoa event system and losing a lot of
conveniences.
Instead, you can just override mouseDown or mouseDragged in your
controls. If your special "rearrange mode" flag is set as you
describe, then you can adjust the frame as the mouse is moved. If the
flag isn't set, you can just call [super mouseDown:] or [super
mouseDragged:]. The advantage of this is Cocoa will send events to the
right control automatically.
I'm not sure how many apps do this, but I can't see any reason it
wouldn't work and certainly isn't a hack. The only downside to this is
that the controls probably need to be subclasses. I'm not sure if that
works for your app.
I don't know of an example that does this offhand, but I hope that
description helps.
Thanks,
- Scott