ページ

2008年5月19日月曜日

ThinButton(その6)ボタンの部品化(ThinButton)

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク

まず ThinButtonの解説から。このクラスは1つのボタンを表しており、表示する画像や大きさ状態を管理する。描画やイベントハンドリングはここでは行わない。

ThinButton.h

enum TB_STATE {
TB_STATE_NORMAL,
TB_STATE_OVER,
TB_STATE_PUSHED
};

@interface ThinButton : NSObject {

NSImage *_image;
NSRect _frame;
UInt _state;
UInt _tag;
}
- (id)initWithImage:(NSImage*)image frame:(NSRect)frame tag:(UInt)tag;
- (BOOL)hitAtPoint:(NSPoint)point;

- (UInt)state;
- (void)setState:(UInt)state;

- (NSImage*)image;
- (NSRect)frame;
- (UInt)tag;

@end



値の保持が目的なので処理らしい処理は特に行っていない。面倒な処理は ThinButtonBar で行なう。主要なメソッド実装を下記に示す。
ThinButton.m
@implementation ThinButton

- (id)initWithImage:(NSImage*)image frame:(NSRect)frame tag:(UInt)tag
{
self = [super init];
if (self) {
_image = [image retain];
_frame = frame;
_tag = tag;
_state = TB_STATE_NORMAL;
}
return self;
}

- (BOOL)hitAtPoint:(NSPoint)point
{
return NSPointInRect(point, _frame);
}