Determining dock position and size programmatically

Member
Posts: 63
Joined: 2005.12
Post: #1
Is there a way to determine the current location and size of the dock (and whether it's currently visible) programmatically? What I'm trying to do is determine a reasonable maximum window size for a game running in windowed mode. I've already accounted for the menu bar, but if possible I'd like to take the dock into account as well.

I'm using C++ and Objective-C, Cocoa, Xcode 3.1.2, and OS X 10.5.6.
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #2
You could try taking a look at com.apple.dock.plist

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #3
Also take a look at -[NSScreen visibleFrame].
Quote this message in a reply
Member
Posts: 63
Joined: 2005.12
Post: #4
Thank you both for your replies. 'visibleFrame' works perfectly, thanks.

One other question, and I think I'll have it. Is there a way to determine the height of the title bar prior to creating a window? I gather you can get this information easily enough if you have an NSWindow instance to work with, but since this is for setting up the window in the first place, there's no instance available.
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #5
_jyk_ Wrote:Is there a way to determine the height of the title bar prior to creating a window? I gather you can get this information easily enough if you have an NSWindow instance to work with, but since this is for setting up the window in the first place, there's no instance available.

Looking at the documentation, it seems like you'd be able to calculate this from +frameRectForContentRect:styleMask: and/or +contentRectForFrameRect:styleMask:. I remember this having been a stumbling block for me in the past, too...
Quote this message in a reply
Member
Posts: 63
Joined: 2005.12
Post: #6
Thanks again for the help - everything seems to be working correctly.

I've written the following support functions:

Code:
void GetMaximizedWindowInfo(int* x, int* y, int* width, int* height)
{
    *x = 0;
    *y = 0;
    *width = 0;
    *height = 0;

    CFDictionaryRef display = CGDisplayCurrentMode(kCGDirectMainDisplay);
    CFNumberGetValue (
        CFDictionaryGetValue(display, kCGDisplayWidth), kCFNumberSInt32Type, width);
    CFNumberGetValue (
        CFDictionaryGetValue(display, kCGDisplayHeight), kCFNumberSInt32Type, height);

    NSArray* screens = [NSScreen screens];
    if (screens != nil) {
        NSScreen* screen = [screens objectAtIndex: 0];
        if (screen != nil) {
            NSRect frameRect = [screen visibleFrame];
            unsigned int style =
                NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask;
            NSRect contentRect =
                [NSWindow contentRectForFrameRect:frameRect styleMask:style];
            *x = (int)frameRect.origin.x;
            *y = (int)frameRect.origin.y;
            *width = (int)contentRect.size.width;
            *height = (int)contentRect.size.height;
        }
    }
}

void SetWindowPosition(int x, int y)
{
    NSWindow* window = [NSApp mainWindow];
    if (window != nil) {
        NSPoint origin;
        origin.x = (float)x;
        origin.y = (float)y;

        [window setFrameOrigin:origin];
    }
}

Which are then used as follows:

Code:
int x, y, width, height;
GetMaximizedWindowInfo(&x, &y, &width, &height);
SDL_SetVideoMode(width, height, 0, SDL_OPENGL);
SetWindowPosition(x, y);

This is the first Objective-C/Cocoa code I've had to write, so if any of you Cocoa experts sees anything amiss, please let me know.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  passing parameters to open -a programmatically savage 3 4,247 Dec 23, 2007 12:01 PM
Last Post: OneSadCookie
  Visual artefacts on OS X Dock weichsel 8 5,673 Sep 23, 2007 06:03 AM
Last Post: weichsel
  Get mouse speed instead of position? Uuugggg 3 3,216 Jun 13, 2007 02:01 PM
Last Post: Uuugggg