iTunes, Mail, and the Finder all have buttons that are displayed in a table view on the right hand side of the cell. When you hover over these buttons, the image used changes to indicate that it's a button. There is really no easy way to add this functionality to your own custom cell. Searching for others who tried to do the same sort of thing pulled up one thread on the coca-dev mailing list with no conclusion to the person's issue.
I just finished adding hoverable buttons to Senuti's source list, and it was very tricky. Here's what I did.
NSWindow subclass for custom event dispatching
NSTableView subclass for custom event dispatching
NSCell to handle event notification from the table view
So the cell needs to respond to mouseMoved: events. NSButtonCell does that, you think, but NSButtonCell doesn't receive the events in an NSTableView. So that's what the NSTableView subclass is for. It handles mouse events and dispatches them to the cell. The cell isn't really part of any control, though, and is reused throughout the rows of the table. Therefore, calling the table delegate method, tableView:willDisplayCell:forTableColumn:row:, before sending messages to the cell was necessary. The cell then responds and lets the table view know whether or not it needs to redraw that cell.
And of course, you have to have the table view become the firstResponder in order to receive mouseMoved: events. If it does become the firstResponder, though, then whenever the mouse enters the table view, focus will change to that view. That's not a good thing. That's where the NSWindow subclass comes into play. Each mouseMoved: event tracked in the window is forwarded to a list of additional responders. The table view adds and removes itself from the additional responders list when the mouse enters/exits.
All that's left to do is draw the button image, and update when the mouse enters/leaves/clicks in the button area. Since this post doesn't go into much detail, the implementation of what I did is all available under the GPL. SEButtonImageTextCell, SEEventTable, and SEEventWindow.
1 Comment Tags: Apple, Cocoa, Code, GPL, Objective-C, OS X, Senuti
There's a nice extension to the Python implementation of Markdown that allows for syntax highlighting of code. It's called CodeHilite, and it allows you to use a couple of different syntax highlighting tools, one of which being Pygments. Pygments and CodeHilite are great except that Objective-C syntax highlighting hasn't been implemented in Pygments.
Fortunately the Pygments team has made it pretty easy to create a new Lexer. I quickly modified the CLexer to create an ObjectiveCLexer. It's not really ideal, but it works. If you have improvements to this Lexer, please email me and/or comment on the ticket I created for the Pygments team.
No Comments Tags: Blog, Code, Pygments, Python
Django's Model class is intended to be subclassed when creating a model for your application. That's about the only intended use. But what if you want to add some extra functionality to Django's model class that all of your models can use?
You would think that something like this:
from django.db import models class M(models.Model): def something_special(self): pass class MyModel(M): field = models.CharField(maxlength=255)
would work just fine, but it doesn't work as desired. There's a good reason why it doesn't. Here's the MySQL code that Django produces for this setup:
BEGIN; CREATE TABLE `myapp_mymodel` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `field` varchar(255) NOT NULL ); CREATE TABLE `myapp_m` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY ); COMMIT;
which helps explain why it doesn't work as desired. Django considers the M class a valid model, one that can be saved to the database, so it creates an entry in the database for it. We don't need or want it in the database, though. We just want to extend the basic model class.
Read More...7 Comments Tags: Code, Django
It has always bothered me that Cocoa doesn't have Regular Expressions built in. Using PCRE 6.7, I've created a small Cocoa Framework that does Regular Expressions. I know there's the AGRegex that's up on sourceforge, but I decided to take my crack at making something that might be a little different. It's not complete yet, but I figured I'd write about it since I just created a subversion repository for it.
Here are the basics:
I chose to call methods on the strings instead of the regex objects because this is a little more like all other object oriented regular expression implementations and because I think it makes more sense that way.
Creating a regular expression is easy as:
PCRegex *regex = [PCRegex regexWithPattern:@"pattern"];
Matching regular expressions against strings is easy, too. With the previous regular expression defined:
#import <ObjCRegex/ObjCRegex.h> ... [@"This is the string that I want to match" match:regex]; [@"This string will match because it contains the word pattern" match:regex];
With connivence methods everything is extremely simple:
#import <ObjCRegex/ObjCRegex.h> ... [@"This is the string that I want to match" matchPattern:@"pattern"]; [@"This is the string that I want to match" matchPattern:@"str.*"];
Of course if you want something more powerful, you're wondering what I did to deal with backreferences. Again, it's pretty simple. Here's an example:
#import <ObjCRegex/ObjCRegex.h> ... NSArray *backrefArray; PCRegex *regex = [PCRegex regexWithPattern:@"([^ ]*) ([^ ]*)"]; [@"first second third" match:regex backreferences:backrefArray]; NSString *first_match = [[backrefArray objectAtIndex:1] string]; NSString *second_match = [[backrefArray objectAtIndex:2] string];
Like many other implementations, the 0th item in the backreference array is the fully matched string, so in the previous example it would be @"first second".
There are some things that are incomplete and/or you should know before you get excited about using it:
So if you're still reading, you may be interested in checking out the code which can be done via:
svn co svn://fadingred.org/objcregex/trunk objcregex
Enjoy!
2 Comments Tags: Cocoa, Code, Objective-C, Regex