//
//  OmView.m
//  misc_test_app
//
//  Created by david van brink on 8/1/10.
//  Copyright 2010 omino.com. All rights reserved.
//

#import "OmView.h"
#import "OmCoreGraphicsUtils.h"


@implementation OmView


- (id)initWithFrame:(CGRect)frame 
{
    self->drawingObject = nil;
    self->drawingMethod = nil;

    if ((self = [super initWithFrame:frame])) 
    {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc {
    [super dealloc];
}


- (void)setController:(UIViewController *)newController
{
    self->controller = newController;
}

- (void)dismiss
{
    [self->controller dismissModalViewControllerAnimated:YES];
}

- (void) setBgRgb:(int)rgb
{
    float r = ((rgb >> 16) & 0x000000ff) / 255.0;
    float g = ((rgb >> 8)  & 0x000000ff) / 255.0;
    float b = ((rgb >> 0)  & 0x000000ff) / 255.0;
    UIColor *bg = [[UIColor alloc] initWithRed:r green:g blue:b alpha:1];
    [bg autorelease];
    [self setBackgroundColor:bg];
}

- (void) repaint
{
    [self setNeedsDisplayInRect:[self bounds]];
}

- (UIButton *)addButton:(NSString *)buttonText tag:(NSInteger)tag rect:(CGRect)r
{
    UIButton *bt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self addSubview:bt];
    [bt setTitle:buttonText forState:UIControlStateNormal];
    [bt setFrame:r];
    [bt addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [bt setTag:tag];
    [bt setTitleColor:GColor(0xe0e0e0) forState:UIControlStateDisabled];
    return bt;
}

- (UIButton *)addButton:(NSString *)buttonText tag:(NSInteger)tag x:(int)x y:(int)y w:(int)w h:(int)h
{
    CGRect r = CGRectMake(x,y,w,h);
    UIButton *bt = [self addButton:buttonText tag:tag rect:r];
    return bt;
}

- (void) delegateDrawing:(id)newDrawingObject selector:(SEL)newDrawingMethod
{
    self->drawingObject = newDrawingObject;
    self->drawingMethod = newDrawingMethod;
}


- (void)drawRect:(CGRect)rect 
{
    if(self->drawingObject && self->drawingMethod)
        objc_msgSend(self->drawingObject, self->drawingMethod,self); 
 //       [self->drawingObject performSelector:self->drawingMethod withObject:self];
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    [self->controller dismissModalViewControllerAnimated:YES];
}



@end

