Browsed by
Category: Mac Development

VMWare Fusion

VMWare Fusion

VMWare has been around for over 10 years.  It’s an amazing piece of technology.  I’ve not had a chance to use it until now.  VMWare let’s you run “virtual” OS s on your computer using virtualization. This is how VMWare defines virtualization:

Today’s x86 computer hardware was designed to run a single operating system and a single application, leaving most machines vastly underutilized. Virtualization lets you run multiple virtual machines on a single physical machine, with each virtual machine sharing the resources of that one physical computer across multiple environments. Different virtual machines can run different operating systems and multiple applications on the same physical computer.

Basically, it means you can run ANY version of Windows on a single Windows PC, or on a single Mac or a single Linux machine.  You can also run any version of Windows on a Mac which is what we have standardized on at TabbedOut. We get 13in Macbook Pros with 4 Meg of memory and a large external monitor.  We then install VMWare and run whatever version of windows we need to on it!  This combined with Spaces for the Mac makes for a pretty nice development environment.

A few notes about VMWare Fusion:

  • To rename a VM, go to VM Library, select the VM you want to rename, select settings, then click on the name to change it in place.
  • To rename the folder containing a VM, remove the VM from the VM Library by selecting delete and removing it from the list but be sure to KEEP the file so you don’t remove it from disk.  Then just rename the folder and reload it back into the VM Library by using the File…Open command
  • To increase the hard drive size of a VM you must delete all snapshots, select hard drive settings and increase the hard drive size. Run the VM and in the OS increase the size of the partition.  For XP you must use a 3rd party app as this is not supported natively.  I used the free EASEUS Home edition.

Explanation of files that make up a VM

XCode Tips and Tricks

XCode Tips and Tricks

Looking up how to change the default __MyCompanyName__ string that is inserted in new files, I came across this link titled Quickies for Xcode.

BTW, this is how to do it.  Enter the following command at a Terminal Prompt:

defaults write PBXCustomTemplateMacroDefinitions '{"ORGANIZATIONNAME" = "NewName";}'

Can’t login to mac from Vista

Can’t login to mac from Vista

For sometime I’ve had problems connecting from my Vista laptop to my MacBook Pro.  I’ve only been  able  to connect  the other way.  Finally a solution.

This thread recommends the following:

On the start menu in the search field type “gpedit.msc”, hit enter. This will open the group policy editor.

Go to “Computer Configruation” –> “Windows Settings” –> “Security Settings” –> “Local Policies” –> “Security Options”

In the pane on the right side of the screen, select “Network Security: LAN Manager Authentication level.” By default this read “Send NTLMv2 response only.”
Change “Send NTLMv2 respone only” to “Send LM & NTLM — use NTLMv2 session security if negotiated”

restart the client

If you use a famillial vista try this.  You must modify the BDR

DWORD named : LmCompatibilityLevel must set to 1

Implementing right-click context menu in Cocoa as a function call

Implementing right-click context menu in Cocoa as a function call

I was porting some Windows code to Cocoa that used a synchronous call to TrackMenuPopup (TPM_RETURNCMD | TPM_NONOTIFY) to display a context menu and immediately return the menu item selected.  I didn't want the menu to post a notification because that would require a more complicated architecture that could port between Mac and Windows.  I posted the question titled "Is there an equivalent technique in Cocoa for the synchronous TrackPopupMenu in Windows?" to

I couldn't decouple the NSMenu notification to an NSView so I came up with a solution using a dummy NSView.  This way I could implement a popup menu as a function call that returns the selected value.  Apparently popUpContextMenu is synchronous:

I ultimately came up with the following solution:

// Dummy View class used to receive Menu Events

@interface DVFBaseView : NSView
    NSMenuItem* nsMenuItem;
– (void) OnMenuSelection:(id)sender;
– (NSMenuItem*)MenuItem;

@implementation DVFBaseView
– (NSMenuItem*)MenuItem
    return nsMenuItem;
– (void)OnMenuSelection:(id)sender
    nsMenuItem = sender;

// Calling Code:

void HandleRButtonDown (NSPoint pt)
    NSRect    graphicsRect;  // contains an origin, width, height
    graphicsRect = NSMakeRect(200, 200, 50, 100);
    // Create Menu and Dummy View
    nsMenu = [[[NSMenu alloc] initWithTitle:@"Contextual Menu"] autorelease];
    nsView = [[[DVFBaseView alloc] initWithFrame:graphicsRect] autorelease];

    NSMenuItem* item = [nsMenu addItemWithTitle:@"Menu Item# 1" action:@selector(OnMenuSelection:) keyEquivalent:@""];
    [item setTag:ID_FIRST];

    item = [nsMenu addItemWithTitle:@"Menu Item #2" action:@selector(OnMenuSelection:) keyEquivalent:@""];
    [item setTag:ID_SECOND];

    // Providing a valid windowNumber is key in getting the Menu to display in the proper location
    int windowNumber = [(NSWindow*)myWindow windowNumber];
    NSRect frame = [(NSWindow*)myWindow frame];
    NSPoint wp = {pt.x, frame.size.height – pt.y};  // Origin in lower left
    NSEvent* event = [NSEvent otherEventWithType:NSApplicationDefined
                        timestamp: (NSTimeInterval) 0
                        windowNumber: windowNumber
                        context: [NSGraphicsContext currentContext]
                        data1: 0
0;           data2: 0];   

    [NSMenu popUpContextMenu:nsMenu withEvent:event forView:nsView];
    NSMenuItem* MenuItem = [nsView MenuItem];
    switch ([MenuItem tag])
    case ID_FIRST: HandleFirstCommand(); break;
    case ID_SECOND: HandleSecondCommand(); break;
Creating Cocoa applications programatically (i.e. NIB-less)

Creating Cocoa applications programatically (i.e. NIB-less)

"Why on earth would you want to create a Cocoa application without using Interface builder?"

That's the basic reaction you get if you try to seek out help online in writing a Cocoa application from scratch.

There may not be a lot of valid reasons but if there is at least one reasonable answer, then the question should be moot.

Our applications at DVFilm are cross-platform.  The user interfaces are very simple.  The majority of the code is written in C/C++ and in the business logic.  So I have not given up on the idea that the simple form-based UI can be written in such a way that the majority can be shared between Windows and Mac. 

There, that's my "reasonable" explanation on why I am crazy enough to consider creating a Cocoa application programmatically.  If that doesn't satisfy, here is another reason.

I have inherited a Carbon application (a QT template app, actually) and simply want to add a single dialog using existing Cocoa code. 

If neither answer satisfies, I DON'T CARE.  I do appreciate all the arguments for stopping my Tilting at Windmills but THEY don't satisfy ME.  If you can't bring yourself to suspend your disbelief and offer help, I'll figure it out on my own. 

It's not like it's NOT SUPPORTED.  And apparently, creating applications in Cocoa programmatically might very be well be the norm for iPhone development.

Here is someone with the same goal.

Notifications in Cocoa

Notifications in Cocoa

Notifications seem unnecessarily confusing the way they are documented.

I want to know when my my window resizes so I notice in the documentation for NSWindow that there is a NSWindowDidResizeNotification.  Great seems that’s what I want to write a handler for.

What do I do?  The documentation does not describe what the handler function signature should be but I locate some sample code that uses:

– (void)windowDidResize:(NSNotification*)theNotification

so I add that to my NSWindow derived class for my main window but the method is not called.

I read up on Notifications and they talk about registering for notifications you are interested in but that seems unnecessary for this particular event, especially since I’ve subclassed the window.  What is it in my background that expects that by simply subclassing the window that, that would be sufficient to capture the resize event?  Is it my Windows background?

Anyway, I added

    [self setDelegate:self]; // Set as delegate in order to receive notifications

in my MainWIndow awakeFromNib handler and my resize handler started getting called.

In another application I had to set the delegate again when adding the resize handler to the NSHandler object for a Window wasn’t sufficient.

"Delegates and Data Sources" in the Cocoa documentation sheds some light on this.

Porting C++ code to Objective-C

Porting C++ code to Objective-C

I’m in the middle of porting a Windows C++ application to the mac.  I’ve posted before that I was pleasantly pleased to find that with some limitations, XCode supports mixing C++ and objective-C code.

Here are a few things to look out for when working with C++ code in a Cocoa application:

1. Missing C++ member functions may not be flagged at compile time and your program will abort with a message similar to:

ZeroLink: unknown symbol ‘__ZN8DVFArray6ExistsEP6DVFObj’

You can use the C++filt program which is part of the GNU Binary Utilities to help demangle the function in error.  I could not build this package so I looked and found a copy of the C++filt program. Unfortunately I could only find a copy for the PC.
The example listed demangles to: DVFArray::Exists(DVFObj*)

2. You cannot store C++ objects/pointers in Cocoa containers.  One workaround is to store NSValue objects in the containers and store C++ pointeres in the NSValue objects:

[myNSArray addObject:[NSValue valueWithPointer:pCPPObj]];
pCPPObj = [[_array objectAtIndex:idx] pointerValue]

3. You should not use static C++ objects or static data members.  Constructors for static objects will be called before main() is called and you will not have an NSAutoReleasePool initialized yet. You will get a message similar to:

*** _NSAutoreleaseNoPool(): Object 0x307f60 of class NSConcreteValue autoreleased with no pool in place – just leaking

The discussion "NSAutoreleasePool and static data member constructors" is very good on this subject

EXC_BAD_ACCESS during startup

EXC_BAD_ACCESS during startup

Once again, the XCODE debugger not only failed to help but got in the way by showing me a stack trace that was not directly relevant to my problem.   The program crashed with EXC_BAD_ACCESS and the stack trace looked like this:

#0    0x90a594c7 in objc_msgSend
#1    0xbffff7b8 in ??
#2    0x932899d8 in loadNib
#3    0x932893d9 in +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:]
#4    0x9328903a in +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:]
#5    0x93288f7c in +[NSBundle(NSNibLoading) loadNibNamed:owner:]
#6    0x93288cc3 in NSApplicationMain
#7    0x00009f80 in main at

This made me think that my NIB was corrupt since I didn’t see anything in the stack trace that pointed to my code. After wasting several hours investigating that rat hole, I decided to try to do what the debugger didn’t help with and that’s track down the offending line.  With breakpoints and Debugger(); calls (breakpoints don’t always work for me) I found that an IBOutlet was not being initialized and I was sending a message to nil.  If the run-time was not going to flag this as an error, why would this cause a problem down the line?  Arrggg! 

Lesson learned, if a stack trace looks similar to the above, then look at your initialization code of your Nib objects.