rm_you: The screen rotate status bar applet does this, and I believe is open source (or at least by a less top-heavy developer). Perhaps it could serve as a decent example?