[Pythonmac-SIG] NSTableView and NSOutlineView backgrounds

Dinu Gherman gherman at darwin.in-berlin.de
Tue Oct 14 07:10:49 EDT 2003

I sighed:

> Oh dear! But ok, thanks! Now, I assume, there must be a *really*
> horrible hack to make this work on the outline column of NSOutline-
> Views, too, since its cells are NSButtonCells (without background
> colors) instead of NSTextFieldCells... Sigh!

As it turns out this is kind of an eternal issue in the Cocoa
mailing lists, driving many people either into solid desparation
or leading them to putting all their hope into Panther (10.3.)
where Apple has finally added some direct support for alterna-
ting background colors.

BUT... drumrolls... using Python there's no need to despair! I've
come up with a working solution that is probably the shortest one
you can have (with MyOutlineView being subclassed in IB from NS\

   SOFT_BLUE = NSColor.colorWithCalibratedRed_green_blue_alpha_(
       0.92941, 0.95294, 0.99607, 1.0)

   class MyOutlineView(NibClassBuilder.AutoBaseClass):
       def drawRow_clipRect_(self, row, rect):
           if row % 2 == 0 and not self.isRowSelected_(row):
               bounds = self.rectOfRow_(row)
               path = NSBezierPath.fillRect_(bounds)
           NSOutlineView.drawRow_clipRect_(self, row, rect)

It draws the entire row background at one, but maybe one can also
change it a bit to draw only one cell, which would leave the grid
(if drawn) untouched, I guess.

For the fun of it you can also use something like NSBezierPath.\
pathWithOvalInRect_ or anything else to draw something really
more unusual... ;-)

The inspiration for this came from Evan Jones' implementation in
Objective-C, which is, of course, several times larger than the
code above, especially since it involves more than only this one


Puh, that was a nice hunt!

Regards and thanks,


Dinu C. Gherman
"An expert is someone who knows some of the worst mistakes that can
be made in his subject, and how to avoid them." (Werner Heisenberg)

More information about the Pythonmac-SIG mailing list