# HG changeset patch # User bsmith@81767d24-ef19-dc11-ae90-00e081727c95 # Date 1315676488 0 # Node ID 8d8c73fb27f4c40c5d732fc40b9bb431320bf2b1 # Parent 1c63bf62e0b0c3aebe44b85017d0e37a0367f497 Initial implementation of printing support on Mac. Just getting a blank page right now, but wanted to commit. diff -r 1c63bf62e0b0 -r 8d8c73fb27f4 mac/dw.m --- a/mac/dw.m Fri Sep 09 21:05:57 2011 +0000 +++ b/mac/dw.m Sat Sep 10 17:41:28 2011 +0000 @@ -9263,6 +9263,14 @@ return DW_ERROR_NONE; } +typedef struct _dwprint +{ + NSPrintInfo *pi; + int (*drawfunc)(HPRINT, HPIXMAP, int, void *); + void *drawdata; + unsigned long flags; +} DWPrint; + /* * Creates a new print object. * Parameters: @@ -9275,10 +9283,42 @@ */ HPRINT API dw_print_new(unsigned long flags, unsigned int pages, void *drawfunc, void *drawdata) { - return NULL; -} - -/* + DWPrint *print; + NSPrintPanel *panel; + PMPrintSettings settings; + NSPrintInfo *pi; + + if(!drawfunc || !(print = calloc(1, sizeof(DWPrint)))) + { + return NULL; + } + + print->drawfunc = drawfunc; + print->drawdata = drawdata; + print->flags = flags; + + /* Get the page range */ + pi = [NSPrintInfo sharedPrintInfo]; + settings = [pi PMPrintSettings]; + PMSetPageRange(settings, 1, pages); + PMSetFirstPage(settings, 1, true); + PMSetLastPage(settings, pages, true); + [pi updateFromPMPrintSettings]; + + /* Create and show the print panel */ + panel = [NSPrintPanel printPanel]; + if(!panel || [panel runModalWithPrintInfo:pi] == NSCancelButton) + { + free(print); + return NULL; + } + /* Put the print info from the panel into the operation */ + print->pi = pi; + + return print; +} + +/* * Runs the print job, causing the draw page callbacks to fire. * Parameters: * print: Handle to the print object returned by dw_print_new(). @@ -9288,7 +9328,68 @@ */ int API dw_print_run(HPRINT print, unsigned long flags) { - return DW_ERROR_UNKNOWN; + DWPrint *p = print; + NSBitmapImageRep *rep; + NSPrintInfo *pi; + NSPrintOperation *po; + HPIXMAP pixmap; + NSImage *image; + NSImageView *iv; + NSSize size; + PMPrintSettings settings; + int x; + UInt32 start, end; + + if(!p) + return DW_ERROR_UNKNOWN; + + /* Figure out the printer/paper size */ + pi = p->pi; + size = [pi paperSize]; + /* Create an image view to print and a pixmap to draw into */ + iv = [[NSImageView alloc] init]; + pixmap = dw_pixmap_new(iv, size.width, size.height, 8); + rep = pixmap->image; + + /* Create an image with the data from the pixmap + * to go into the image view. + */ + image = [[NSImage alloc] initWithSize:[rep size]]; + [image addRepresentation:rep]; + [iv setImage:image]; + + /* Create the print operation using the image view and + * print info obtained from the panel in the last call. + */ + po = [NSPrintOperation printOperationWithView:iv printInfo:pi]; + [po setShowsPrintPanel:NO]; + + /* Get the page range */ + settings = [pi PMPrintSettings]; + PMGetFirstPage(settings, &start); + if(start) + start--; + PMGetLastPage(settings, &end); + PMSetPageRange(settings, 1, 1); + PMSetFirstPage(settings, 1, true); + PMSetLastPage(settings, 1, true); + [pi updateFromPMPrintSettings]; + + /* Cycle through each page */ + for(x=start; xdrawfunc; x++) + { + /* Call the application's draw function */ + p->drawfunc(print, pixmap, x, p->drawdata); + /* Print the image view */ + [po runOperation]; + /* Fill the pixmap with white in case we are printing more pages */ + dw_color_foreground_set(DW_CLR_WHITE); + dw_draw_rect(0, pixmap, TRUE, 0, 0, (int)size.width, (int)size.height); + } + /* Free memory */ + dw_pixmap_destroy(pixmap); + free(p); + return p->drawfunc ? DW_ERROR_NONE : DW_ERROR_UNKNOWN; } /* @@ -9298,5 +9399,9 @@ */ void API dw_print_cancel(HPRINT print) { -} - + DWPrint *p = print; + + if(p) + p->drawfunc = NULL; +} +