Search code examples
cocoacustom-controlshexdump

Implementing custom NSView in Cocoa on OS X


I am creating a Cocoa application wherein one view will contain the "hex dump" of the currently loaded document. Up until this point, I have been using a bog-standard (and very unappealing) NSTextField, but I am now looking for something more powerful.

I am a great fan of 0xED.app and would love to replicate its main "hex dump" view. How would I go about doing this?

I'm not necessarily after the eye-candy, but the ability to select a range of bytes without also selecting the offset or text columns. I am a loss as to where I would even begin to implement this effectively. Surely this is not drawn upon a blank canvas?

0xED screenshot


Solution

  • To get started and see how things basically work:

    1. Subclass NSView.
    2. Add an instance variable to hold your NSData.
    3. Override drawRect:

    This approach will be slow for a large amount of data, but will give you a good handle on implementing a NSView subclass. After that, you'll want to improve the drawing performance by implementing something better than repeated calls to draw strings one at a time. You'll also want to implement overrides of methods like mouseDown: and keyDown: to handle input from the user to allow things like selecting a range of bytes.

    That should get you started, once you have that going, I'd suggest asking follow up questions.