comparison ios/dw.m @ 2726:7a15401e73f4

iOS: Initial print support for iOS.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sat, 11 Dec 2021 00:54:01 +0000
parents 2dd68d2810ca
children f9df10c66353
comparison
equal deleted inserted replaced
2725:2dd68d2810ca 2726:7a15401e73f4
934 [super dealloc]; 934 [super dealloc];
935 } 935 }
936 -(BOOL)acceptsFirstResponder { return YES; } 936 -(BOOL)acceptsFirstResponder { return YES; }
937 @end 937 @end
938 938
939 /* Subclass page renderer to implement page by page rendering */
940 @interface DWPrintPageRenderer : UIPrintPageRenderer
941 {
942 int (*drawfunc)(HPRINT, HPIXMAP, int, void *);
943 void *drawdata;
944 unsigned long flags;
945 unsigned int pages;
946 HPRINT *print;
947 }
948 -(void)setDrawFunc:(void *)input;
949 -(void)setDrawData:(void *)input;
950 -(void *)drawData;
951 -(void)setFlags:(unsigned long)input;
952 -(unsigned long)flags;
953 -(void)setPages:(unsigned int)input;
954 -(void)setPrint:(HPRINT)input;
955 @end
956
957 @implementation DWPrintPageRenderer
958 -(void)setDrawFunc:(void *)input { drawfunc = input; }
959 -(void)setDrawData:(void *)input { drawdata = input; }
960 -(void *)drawData { return drawdata; }
961 -(void)setFlags:(unsigned long)input { flags = input; }
962 -(unsigned long)flags { return flags; }
963 -(void)setPages:(unsigned int)input { pages = input; }
964 -(void)setPrint:(HPRINT)input { print = input; }
965 -(void)drawContentForPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)contentRect
966 {
967 HPIXMAP pm = dw_pixmap_new(nil, contentRect.size.width, contentRect.size.height, 32);
968 DWImage *image = pm->image;
969 drawfunc(print, pm, (int)pageIndex, drawdata);
970 [[image image] drawInRect:contentRect];
971 dw_pixmap_destroy(pm);
972 }
973 -(NSInteger)numberOfPages { return (NSInteger)pages; }
974 @end
975
939 @interface DWFontPickerDelegate : UIResponder <UIFontPickerViewControllerDelegate> 976 @interface DWFontPickerDelegate : UIResponder <UIFontPickerViewControllerDelegate>
940 { 977 {
941 DWDialog *dialog; 978 DWDialog *dialog;
942 } 979 }
943 -(void)fontPickerViewControllerDidPickFont:(UIFontPickerViewController *)viewController; 980 -(void)fontPickerViewControllerDidPickFont:(UIFontPickerViewController *)viewController;
1211 iResponse = DW_POINTER_TO_INT(dw_dialog_wait(dialog)); 1248 iResponse = DW_POINTER_TO_INT(dw_dialog_wait(dialog));
1212 /* Once the dialog is gone we can rehide our window */ 1249 /* Once the dialog is gone we can rehide our window */
1213 [hiddenWindow resignKeyWindow]; 1250 [hiddenWindow resignKeyWindow];
1214 [hiddenWindow setHidden:YES]; 1251 [hiddenWindow setHidden:YES];
1215 [params addObject:[NSNumber numberWithInteger:iResponse]]; 1252 [params addObject:[NSNumber numberWithInteger:iResponse]];
1253 }
1254 -(void)printDialog:(NSMutableArray *)params
1255 {
1256 __block DWDialog *dialog = dw_dialog_new(NULL);
1257 UIPrintInteractionController *pc = [UIPrintInteractionController sharedPrintController];
1258 UIPrintInfo *pi = [params firstObject];
1259 DWPrintPageRenderer *renderer = [params objectAtIndex:1];
1260 /* Not currently using the passed in flags, but when we need them...
1261 NSNumber *flags = [params objectAtIndex:2];*/
1262 int result = DW_ERROR_UNKNOWN;
1263
1264 /* Setup our print dialog */
1265 [pc setPrintInfo:pi];
1266 [pc setPrintPageRenderer:renderer];
1267
1268 /* Present the shared printer dialog */
1269 [pc presentAnimated:YES completionHandler:^(UIPrintInteractionController * _Nonnull printInteractionController, BOOL completed, NSError * _Nullable error) {
1270 dw_dialog_dismiss(dialog, DW_INT_TO_POINTER((completed ? DW_ERROR_NONE : DW_ERROR_UNKNOWN)));
1271 }];
1272 result = DW_POINTER_TO_INT(dw_dialog_wait(dialog));
1273
1274 [params addObject:[NSNumber numberWithInteger:result]];
1216 } 1275 }
1217 -(void)safeCall:(SEL)sel withObject:(id)param 1276 -(void)safeCall:(SEL)sel withObject:(id)param
1218 { 1277 {
1219 if([self respondsToSelector:sel]) 1278 if([self respondsToSelector:sel])
1220 { 1279 {
10882 * Returns: 10941 * Returns:
10883 * A handle to the print object or NULL on failure. 10942 * A handle to the print object or NULL on failure.
10884 */ 10943 */
10885 HPRINT API dw_print_new(const char *jobname, unsigned long flags, unsigned int pages, void *drawfunc, void *drawdata) 10944 HPRINT API dw_print_new(const char *jobname, unsigned long flags, unsigned int pages, void *drawfunc, void *drawdata)
10886 { 10945 {
10887 /* TODO: Implement printing on iOS */ 10946 if(drawfunc)
10947 {
10948 UIPrintInfo *pi = [UIPrintInfo alloc];
10949 DWPrintPageRenderer *renderer = [DWPrintPageRenderer new];
10950 NSMutableArray *pa = [[[NSMutableArray alloc] initWithObjects:pi, renderer, nil] retain];
10951
10952 if(!jobname)
10953 jobname = "Dynamic Windows Print Job";
10954
10955
10956 [pi setOutputType:UIPrintInfoOutputGeneral];
10957 [pi setJobName:[NSString stringWithUTF8String:jobname]];
10958 [pi setOrientation:UIPrintInfoOrientationPortrait];
10959 [renderer setDrawFunc:drawfunc];
10960 [renderer setDrawData:drawdata];
10961 [renderer setPages:pages];
10962 [renderer setFlags:flags];
10963 [renderer setPrint:pa];
10964 return pa;
10965 }
10888 return NULL; 10966 return NULL;
10889 } 10967 }
10890 10968
10891 /* 10969 /*
10892 * Runs the print job, causing the draw page callbacks to fire. 10970 * Runs the print job, causing the draw page callbacks to fire.
10896 * Returns: 10974 * Returns:
10897 * DW_ERROR_UNKNOWN on error or DW_ERROR_NONE on success. 10975 * DW_ERROR_UNKNOWN on error or DW_ERROR_NONE on success.
10898 */ 10976 */
10899 int API dw_print_run(HPRINT print, unsigned long flags) 10977 int API dw_print_run(HPRINT print, unsigned long flags)
10900 { 10978 {
10901 /* TODO: Implement printing on iOS */ 10979 int retval = DW_ERROR_UNKNOWN;
10902 return DW_ERROR_UNKNOWN; 10980
10981 if(print)
10982 {
10983 NSMutableArray *pa = print;
10984
10985 [pa addObject:[NSNumber numberWithUnsignedLong:flags]];
10986 [DWObj safeCall:@selector(printDialog:) withObject:pa];
10987 retval = (int)[[pa lastObject] integerValue];
10988 [pa release];
10989 }
10990 return retval;
10903 } 10991 }
10904 10992
10905 /* 10993 /*
10906 * Cancels the print job, typically called from a draw page callback. 10994 * Cancels the print job, typically called from a draw page callback.
10907 * Parameters: 10995 * Parameters:
10908 * print: Handle to the print object returned by dw_print_new(). 10996 * print: Handle to the print object returned by dw_print_new().
10909 */ 10997 */
10910 void API dw_print_cancel(HPRINT print) 10998 void API dw_print_cancel(HPRINT print)
10911 { 10999 {
10912 /* TODO: Implement printing on iOS */ 11000 if(print)
11001 {
11002 NSMutableArray *pa = print;
11003 UIPrintInteractionController *pc = [UIPrintInteractionController sharedPrintController];
11004
11005 [pc dismissAnimated:YES];
11006 [pa release];
11007 }
10913 } 11008 }
10914 11009
10915 /* 11010 /*
10916 * Converts a UTF-8 encoded string into a wide string. 11011 * Converts a UTF-8 encoded string into a wide string.
10917 * Parameters: 11012 * Parameters: