PDA

View Full Version : calling built in objects




Darkroom
Nov 9, 2007, 09:14 AM
how can i call a built in window? (About Window, Color Picker, etc.)... it seems i can only have full control over windows that are included as objects in my NIB...

for example, if i wanted to set a key equivalent to open the built in [NSColorPanel sharedColorPanel], how can i add the built in color panel (or color picker) as an outlet, or as an object to the NIB?

another example: if i remove the "About Application" menu item (and therefore have now automatic way to set a key equivalent), how do i call the built in About Window with a key event (without having to make my own)?



kainjow
Nov 9, 2007, 10:41 AM
for example, if i wanted to set a key equivalent to open the built in [NSColorPanel sharedColorPanel], how can i add the built in color panel (or color picker) as an outlet, or as an object to the NIB?

You don't add it as an outlet. You create a button or a menu and have its action open the color panel.

another example: if i remove the "About Application" menu item (and therefore have now automatic way to set a key equivalent), how do i call the built in About Window with a key event (without having to make my own)?

Use NSApplication's orderFrontStandardAboutPanel: method.

Darkroom
Nov 9, 2007, 03:28 PM
what i'm trying to do basically is call the colorpanel/aboutwindow in an if statement and have the windows do something when the user closes them... but i can't seem to find the correct way of writing it because i can't add them to my NIB, as they're built in objects (or whatever):

the following totally doesn't work, as i'm sure by reading it you would have guessed:


- (void)onCloseOfColorPanel
{
if ([[NSColorPanel sharedColorPanel] performClose]){
[[NSColorPanel sharedColorPanel] setAlphaValue:0.5];
}
}


i know i'm way off, so any help would be appreciated...

HiRez
Nov 9, 2007, 04:00 PM
NSColorPanel is a subclass of NSPanel, which is a subclass of NSWindow, so (in theory) you should be able to catch delegate methods for when that window closes. For example:

// This would probably go in your awakeFromNib:, or wherever,
// just make sure the nib objects are valid first.
// Self = the same class you want the delegate methods delivered to,
// might be your app controller class.
[[NSColorPanel sharedColorPanel] setDelegate:self];

// This method should get called when the panel closes.
// You might want to do a check and just make sure it's really
// the shared color panel that called it.
- (void)windowWillClose:(NSNotification*)note {
// do whatever you want here
}

Warning: this is just off the top of my head, totally untested and no idea if it works, just something for you to try.

Darkroom
Nov 9, 2007, 04:32 PM
nope... still can't get it to work...

HiRez
Nov 9, 2007, 06:15 PM
nope... still can't get it to work...I tried it and it seems to work. I'm attaching a previous color panel related project I added the code to. Choose "Select Color" from the Test menu to bring up the color picker, then watch the Xcode log or Console window when you close the color panel. You should see "windowWillClose:" in the log each time you close the panel.

90825

Darkroom
Nov 10, 2007, 02:25 AM
thanks for the code... i see the log change when the color panel window is closed... but i still can't get it to work for me.. haha...

let's say i want the [NSColorPanel sharedColorPanel] to suddenly appear if the [NSColorPanel sharedColorPanel] is closed, to make it look like you can't close it


- (void)windowWillClose:(NSNotification*)note {
// do whatever you want here
[[NSColorPanel sharedColorPanel] makeKeyAndOrderFront:nil];
}


the panel doesn't reappear when i close the color panel window... essentially what i'm trying to do exactly, is set my fade out code to this (void)windowWillClose, so that when the NSColorPanel (a built in panel that i can't seem to access from my NIB) does close, it will fade out instead of simply just close. i have no problem fading the NSColorPanel in when it's opened, because the opening action is accessed thru a Menu Item in the NIB, and i have no problem fading in and out my main NSWindow, again because i can access it in the NIB... but i just can't access the close actions for the NSColorPanel and NSStandardAboutWindow correctly...

i hope i'm being clear... i'm fairly new at cocoa and still don't have all the terminology down yet...

if you do understand, can you tell me what am i doing wrong?

robbieduncan
Nov 10, 2007, 10:34 AM
I assume the problem is that instead of fading out the panel just disappears right?

You probably need to use a combination of windowShouldClose: and windowWillClose:

If you return NO from windowShouldClose this will prevent the window simply just closing straight away. In the in windowWillClose start your fade. Once the fade is completed order the window out.

Gelfin
Nov 10, 2007, 11:57 AM
You cannot interfere with closing from windowWillClose. That's what windowShouldClose is for. You only need a windowShouldClose handler. Something like this:


- (BOOL)windowShouldClose:(id)window
{
if([window alpha] > 0.0)
{
//set your fade timer
return NO;
}
return YES;
}


Then once your fade timer event notes that the window is transparent, have it send [window close]. Your windowShouldClose event won't be triggered again, because [window close] doesn't ask permission.

Darkroom
Nov 10, 2007, 12:49 PM
still no luck with this... the window fades in fine, but fade out isn't being called; the window simply closes... any more ideas?


- (id)init {
if (self = [super init]) {
[[NSColorPanel sharedColorPanel] setDelegate:self];
}
return self;
}

- (IBAction)selectColor:(id)sender {
[[NSColorPanel sharedColorPanel] makeKeyAndOrderFront:nil];
[[NSColorPanel sharedColorPanel] setAlphaValue:0.0];
timer = [[NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(colorPanelFadeIN:) userInfo:nil repeats:YES] retain];
}

- (void)colorPanelFadeIN:(NSTimer *)theTimer
{
if ([[NSColorPanel sharedColorPanel] alphaValue] < 1.0) {
[[NSColorPanel sharedColorPanel] setAlphaValue:[[NSColorPanel sharedColorPanel] alphaValue] + 0.1];
}
else
{
[timer invalidate];
[timer release];
timer = nil;
}
[[NSColorPanel sharedColorPanel] setTarget:myColorSelection];
[[NSColorPanel sharedColorPanel] setAction:@selector(changeColor:)];
}

- (void)windowWillClose:(NSNotification *)theNotification
{
[[NSColorPanel sharedColorPanel] setAlphaValue:1.0];
timer = [[NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(colorPanelFadeOUT:) userInfo:nil repeats:YES] retain];
}

- (void)colorPanelFadeOUT:(NSTimer *)theTimer
{
if ([[NSColorPanel sharedColorPanel] alphaValue] > 0.0) {
[[NSColorPanel sharedColorPanel] setAlphaValue:[[NSColorPanel sharedColorPanel] alphaValue] - 0.1];
}
else
{
[timer invalidate];
[timer release];
timer = nil;

[[NSColorPanel sharedColorPanel] close];
}
}

Gelfin
Nov 10, 2007, 12:57 PM
You're still trying to invoke your fade from windowWillClose. This will never work, as it is impossible to stop the window from closing at that point. Use windowShouldClose instead.

Darkroom
Nov 10, 2007, 01:22 PM
ok... this is beginning to work... but the problem it seems is that with a (bool) statement i can not include my NSTimer code from my previous (void) statements, therefore i can't set NSTimer scheduledTimerWithTimeInterval:0.03...

finally, this code will only reduce the windows alphaValue by -0.1 and then stop... so how can i make it repeat until it reaches an alphaValue of 0.0, return YES; and therefore closes the panel window?


- (BOOL)windowShouldClose:(id)window
{
if([[NSColorPanel sharedColorPanel] alphaValue] > 0.0)
{
[[NSColorPanel sharedColorPanel] setAlphaValue:[[NSColorPanel sharedColorPanel] alphaValue] - 0.1];
return NO;
}
return YES;
[[NSColorPanel sharedColorPanel] close];
}

Gelfin
Nov 10, 2007, 02:27 PM
ok... this is beginning to work... but the problem it seems is that with a (bool) statement i can not include my NSTimer code from my previous (void) statements, therefore i can't set NSTimer scheduledTimerWithTimeInterval:0.03...

I do not understand what you mean by this, but it strongly sounds as if you have misunderstood something. What do you think the return type of the method has to do with whether or not you can set a timer in it?

finally, this code will only reduce the windows alphaValue by -0.1 and then stop... so how can i make it repeat until it reaches an alphaValue of 0.0, return YES; and therefore closes the panel window?

Well, yes, because that's what you told it to do. Here's the simplest code I can put together to do what you want:


- (BOOL)windowShouldClose:(id)window
{
// Set the timer to do the fading.
[NSTimer scheduledTimerWithTimeInterval:0.03
target:self
selector:@selector(fadeOut:)
// Pass in the window to be faded as userInfo.
userInfo:window
repeats:YES];
// Return NO to prevent the window from closing now.
return NO;
}

- (void)fadeOut:(NSTimer*)timer
{
// Get the window we passed in as userInfo.
NSWindow* window = [timer userInfo];
// Reduce the alpha value by one step.
[window setAlphaValue:[window alphaValue]-0.1];
// Check if the window is transparent.
if([window alphaValue] <= 0.0)
{
// If it is, stop the timer.
[timer invalidate];
// Now close the window.
[window close];
}
}


I can verify that any window that delegates to an object with this code will fade out instead of closing immediately. I checked it against the shared color panel just to be sure.

Darkroom
Nov 10, 2007, 03:02 PM
thanks so much for all your help... it's working perfectly now...

thanks again