//
//  AirPrint.m
//  traceWrite
//
//  Created by 今井 彰彦 on 11/12/18.
//  Copyright (c) 2011 AIG-Soft. All rights reserved.
//

#import "AirPrint.h"
#import "main.h"
#import "libs.h"

@implementation AirPrint

-(void)initPrint:(UIBarButtonItem *)button
{
	printSelf();
	printController = [UIPrintInteractionController sharedPrintController];
	actionButton = button;
}

// .delegateプロパティー式で呼び出される関数
// 必ずinitPrintの後に設定すること
-(void)setDelegate:(id<UIPrintInteractionControllerDelegate>)delegate_
{
	printSelf();
	printController.delegate=delegate_;
}

-(void)setItem:(UIImage *)image jobName:(NSString *)jobName
{
	printSelf();
	// PrintInfo の取得と各種プロパティの設定；印刷情報の設定
	UIPrintInfo *printInfo	= [UIPrintInfo printInfo];
	printInfo.outputType	= UIPrintInfoOutputPhoto;	// 印刷サイズ(A6)
	printInfo.jobName		= jobName;
	printInfo.duplex		= UIPrintInfoDuplexNone;	// 印刷面:None=片面
	printController.printingItem=image;
	// 紙の向き
	if (image.size.width > image.size.height) {
		printInfo.orientation = UIPrintInfoOrientationLandscape;
	} else {
		printInfo.orientation = UIPrintInfoOrientationPortrait;
	}
	
	void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
	^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
	{
	//	self.content = nil;
		if (!completed && error) {
			nslog(@"FAILED! due to error in domain %@ with error code %d",error.domain, (int)error.code);
		}
	};

	// 表示を開く
	if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
		// iPadはpopover
		[printController presentFromBarButtonItem:actionButton animated:YES completionHandler:completionHandler];
	} else {
		// iPhoneはnavi
		[printController presentAnimated:YES completionHandler:completionHandler];
	}
}

-(void)close
// 表示を消す
{
	[printController dismissAnimated:YES];
}

@end
