How to use [NSWindow initWithWindowRef] properly?
How to use [NSWindow initWithWindowRef] properly?
- Subject: How to use [NSWindow initWithWindowRef] properly?
- From: Evan Gross <email@hidden>
- Date: Fri, 07 Dec 2001 20:29:29 -0500
Hi all,
For various reasons (related to problems with NSPanel-as-utility-windows
deactivating the currently active window whenever you click the title bar)
I've been using NSWindow's initWithWindowRef to implement a Carbon utility
window in my Cocoa app. This deactivation problem does not occur with a
Carbon Utility window.
What I'd _REALLY_ like to be able to do is somehow install a Cocoa NSView
into this window's contentView and use Cocoa UI elements/controls. I made a
brief attempt at this, but had no success. I'm guessing it's NOT a
possibility. I'd love to be proven wrong, though. It would make life a lot
easier if I could use Cocoa views in this window somehow...
There's basically no documentation on how one is supposed to use this
feature "properly", so I've got a few questions that someone hopefully knows
the answer to...
Briefly, I've done this:
1. Created a Carbon nib file defining my Carbon window and controls.
2. I've subclassed NSWindowController to "deal with" this window.
3. In my NSWindowController subclass' -init method I:
i) call [super initWithWindow:nil],
ii) create the Carbon WindowRef with CreateWindowFromNib,
iii) install a Carbon Event Handler for the window to handle the
HICommands assigned to the Carbon controls in the it. I also handle (
kEventClassWindow, kEventWindowClose } to orderOut the window instead of
closing it (i.e. I just want to hide it, not dispose of it),
iv) do some basic control initialization (one of the controls is a
DataBrowser) and
v) call [self setWindow:[[NSWindow allocWithZone:[self zone]]
initWithWindowRef:theWindowRef]] to set my window controller's window.
As well, my window contains an EditText control, and sometimes I make it the
"key" window by calling (Carbon's) SetUserFocusWindow. I seem to remember
that calling makeKeyWindow didn't work for me. I relinquish the focus by
calling SetUserFocusWIndow((kUserFocusAuto).
Basically, this seems to work - except for a few odd DataBrowser-related
things. I have no idea if the odd things I'm seeing are a result of
something I'm doing wrong, or (quite conceivably) DataBrowser bugs. The main
oddity is that double-clicks in my DataBrowser often select the row I
double-clicked, then briefly select the row about 5 rows below it, then the
clicked-on row again.
But the whole concept raises a few questions - like whether to use Carbon
calls or NSWindow or NSWindowController methods to perform certain actions,
along with what Cocoa methods are "safe" to use and what (if anything) I
should avoid.
- FIRST: Is there any way to use Cocoa views/controls in a Carbon NSWindow?
If the answer is yes, then the following questions may no longer be
relevant. If not, then...
- Should I be using NSWindow/Controller's orderFront/orderOut/showWindow
etc. methods or Carbon's Show/HideWindow?
- A little "snooping" with the NeoData's Self-Introspector reveals that
there are internal NSCarbonWindowxxx subclasses to implement Carbon
NSWindows and their contentView. It appears that these subclasses install
Carbon Event handlers of their own. SO the question is: what should I/can
I/shouldn't I be implementing in my own Carbon Event handler for my window?
Do I need to handle { kEventClassCommand, kEventProcessCommand } myself, or
is there already some sort of mechanism in-place to do so?
- Is there an issue with the different co-ordinate systems? Or port-related
issues that might explain the strange DataBrowser thing I'm seeing (maybe
mouse down events aren't always converted from screen/global to local/port
co-ordinates quite right?).
Any details on the "right" way to implement a Carbon NSWindow and
NSWindowController would be appreciated. What's possible? What isn't?
Hopefully I'm just not doing something quite right. I can't reproduce any
problems I'm seeing with a straight-up Carbon application.
Thanks,
Evan Gross
Rainmaker Research Inc.