/*
 *  OmUiUtils.c
 *  OmLoc
 *
 *  Created by david van brink on 7/27/10.
 *  Copyright 2010 omino.com. All rights reserved.
 *
 */

#include "OmUiUtils.h"
#import <UIKit/UIKit.h>

/*
 * modal alert with printf-style contents
 */
void OmAlertf(char *title,char *fmt,...)
{
    char s[500];
    va_list v;
    
    va_start(v,fmt);
    vsprintf(s,fmt,v);
    
    NSString *mNs = [NSString stringWithCString:s encoding:0];
    NSString *titleNs = [NSString stringWithCString:title encoding:0];
    UIAlertView *alert = [UIAlertView alloc];
    [alert initWithTitle:titleNs message:mNs delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
    [alert autorelease];
    [alert show];
}



/*
 * Bring up a modal alert to inquire, Yes/No for saving, deleting, &c
 * Return the UIAlertView pointer, so your delegate can know which
 * alert it is getting the response for.
 */
UIAlertView *OmAlertAskNoYes(NSObject<UIAlertViewDelegate> *alertViewDelegate,char *title,NSString *no,NSString *yes,char *fmt,...)
{
    char s[500];
    va_list v;
    
    va_start(v,fmt);
    vsprintf(s,fmt,v);
    
    NSString *mNs = [NSString stringWithCString:s encoding:0];
    NSString *titleNs = [NSString stringWithCString:title encoding:0];
    UIAlertView *alert = [UIAlertView alloc];
    [alert initWithTitle:titleNs message:mNs delegate:alertViewDelegate cancelButtonTitle:no otherButtonTitles:yes,nil];
    [alert autorelease];
    [alert show];
    
    return alert;
}

/*
 * Give a confirmation dialog in a view which *must be* the action delegate as well
 */
UIActionSheet *OmActionAskNoYes(UIView<UIActionSheetDelegate> *actionSheetDelegate,NSString *cancelReturns1,NSString *destructReturns0,char *fmt,...)
{
    char s[500];
    va_list v;
    
    va_start(v,fmt);
    vsprintf(s,fmt,v);
    
    NSString *mNs = [NSString stringWithCString:s encoding:0];
    UIActionSheet *actionSheet = [UIActionSheet alloc];
    [actionSheet initWithTitle:mNs 
                    delegate:actionSheetDelegate 
                    cancelButtonTitle:cancelReturns1 
                    destructiveButtonTitle:destructReturns0 
                    otherButtonTitles:nil];
    [actionSheet autorelease];
    [actionSheet showInView:actionSheetDelegate];
    
    return actionSheet;
}

bool touchInRect(NSSet *touches,UIView *v,int x,int y,int w,int h)
{
	UITouch *touch = [touches anyObject];
    CGPoint pt = [touch locationInView:v];
    if(pt.x < x || pt.y < y || pt.x > (x + w) || pt.y > (y + h))
        return false;
    
    return true;
}
