My OS X Programming Blog
Mac OS X Cocoa and CoreMIDI Programming
About Andrew Choi

MIDI Programs

MIDI File Player (External Device)

MIDI Destination Pop-Up Button

MIDI File Player (Internal Synth)

MusicSequence Sample Code

MIDI File Writer

MIDI Name Document Parser


Fish Creek MIDI Framework



Other Programs


FCBlog and Patch

Chinese Checkers Program

jyut6 ping3 Cantonese Input Method

Cocoa Sample Programs

Syntax Coloring Using Flex

NSTextField and Undo


Implementing File Import

Launch Application and Open URL

Saving Uncommitted Text Field Edits


Jazz Chord Analysis as Optimization

Optimal Line Breaking for Music

Optimal Chord Spacing


A blog where I will write mostly about programming in Cocoa and CoreMIDI, and experiences from my ports of Emacs and XEmacs to the Mac OS.

Multiple Bar Selections
Saturday May 1, 2004

I wrote some code to handle selection of one of more bars in the chord editor. To select a bar, click anywhere on its notation (the slashes). Clicking on one of the chord names will of course select the chord for editing.

To select a number of bars, click on the notation of the starting (or ending) bar and drag and release the mouse in the ending (or starting) bar.

The scroll view will “auto-scroll” if necessary if the mouse is dragged outside the window. This is such a nice Cocoa feature enabled by a single line of code! The focus ring can have a variety of shapes and is interesting to program.

Thursday April 29, 2004

The Cocoa undo manager turns out not to be too difficult to learn. I skimmed its documentation, then the articles whose links I posted yesterday, and then I carefully read its documentation from beginning to end. The problem with articles on the Web is sometimes their authors can be too fixated on their solutions of specialized problems. The quality and generality of these “solutions” can also be questionable sometimes.

I went back to work on the FCBlogEditor (described earlier here, and later improved) to make it support undo and redo. Although it uses an NSTextView object for editing, changes to text through calls to its corresponding NSTextStorage object must be bracketed by

  [textView shouldChangeTextInRange:... replacementString:...];
  [[textView textStorage] replaceCharactersInRange:... withString:...];
  [textView didChangeText];
for undo and redo to work correctly. Here’s a new version of FCBlogEditor that includes this and other small changes I have made since it was last posted.

There is also a well-known problem with using NSTextField with the undo manager (see this article, for example). Basically it doesn’t work as expected if one simply calls setAllowUndo:YES for its field editor. I’ve written some sample code that implements undo/redo for text fields. It assumes that the unit of undo/redo is the combined changes made to a text field between the times it becomes and resigns as first responder.

NSFormatter SubClass for Chord Entry
Wednesday April 28, 2004

I implemented an NSFormatter subclass for chord entry today. With it, only valid chords can be entered into the text fields. Combined with text completion, and navigation using arrow keys, a chord chart can be entered very quickly. For example the first eight bars of A Night in Tunisia can be entered by typing:

eb7 Cmd-→
dm6 Cmd-→
eb7 Cmd-→
dm6 Cmd-→
eb7 Cmd-→
dm6 Cmd-→
em7b5 Opt-→
a7b5 F5 ↓ Opt-→
dm6 Cmd-→

Notice that the formatter will automatically capitalize the chord keys (of course one may also enter the capital letters). It will also turn the letter “b” and the hash mark “#” at suitable locations into a flat and sharp, respectively. Note also that the command right arrow key jumps to the first chord of the next bar and the option right arrow key jumps to the chord at the next half note. Of course you can instead hit the tab key or right arrow key a number of times to get where you want. Text completion is used to enter the A7♭5♭9 chord. What’s also interesting is the FMaj7 and DmMaj7 chords can be typed as fmaj7 and dmmaj7 and the formatter will automatically capitalize them correctly for you.

My next step is to implement cut, copy, and paste using the mouse. I’ll be using a bar as the smallest unit on which the program will operate. This is similar to Finale’s mass mover and mass edit tools. First I have to learn about the NSUndoManager. I’ve found an article on MacEdition and another article on this topic. I’ll read them and report back soon.

Navigation Using the Keyboard in Chord Editor
Tuesday April 27, 2004

I wrote some code to allow the user to navigate within a chord chart in the chord editor using the arrow keys. The left and right arrow keys move to the previous and next beats, respectively. So do the tab and shift-tab keys (to be consistent with Cocoa’s selection of text fields). Option left and right arrow keys move to the next half note. Command left and right arrow keys move to the beginning of the previous and next bar, respectively.

The up and down arrow keys move to the previous and next lines. They are interesting to implement because like a word processor with variable width fonts, the horizontal position where the downward or upward movement begins is recorded and each new position is chosen to be as close to that position horizontally as possible.

It was quite simple to put the text completion code into the Chord Editor. Here’s a screenshot of the result.

April 2004
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

May 2004
Sun Mon Tue Wed Thu Fri Sat
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Mar  Jul


Search this blog with


Less-Known Facts About Emacs

Emacs Rants

Chinese Restaurants in Calgary

Calgary/Banff Tourist Attractions

C++ Reading List

Science Fiction Series

Top-10 Reason I Stopped Working on Emacs

Top-10 Types of Questions I Get About Emacs

10 Defining Moments as Programmer


Carbon XEmacs

Emacs for Mac OS X

Copyright © 2003, 2004, 2005 Andrew Choi (Contact Information). Created with FCBlog