comparison mac/dw.m @ 1141:8d8c73fb27f4

Initial implementation of printing support on Mac. Just getting a blank page right now, but wanted to commit.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sat, 10 Sep 2011 17:41:28 +0000
parents e24e5a13ff2c
children b1b23de965d7
comparison
equal deleted inserted replaced
1140:1c63bf62e0b0 1141:8d8c73fb27f4
9261 NSURL *myurl = [NSURL URLWithString:[NSString stringWithUTF8String:url]]; 9261 NSURL *myurl = [NSURL URLWithString:[NSString stringWithUTF8String:url]];
9262 [[NSWorkspace sharedWorkspace] openURL:myurl]; 9262 [[NSWorkspace sharedWorkspace] openURL:myurl];
9263 return DW_ERROR_NONE; 9263 return DW_ERROR_NONE;
9264 } 9264 }
9265 9265
9266 typedef struct _dwprint
9267 {
9268 NSPrintInfo *pi;
9269 int (*drawfunc)(HPRINT, HPIXMAP, int, void *);
9270 void *drawdata;
9271 unsigned long flags;
9272 } DWPrint;
9273
9266 /* 9274 /*
9267 * Creates a new print object. 9275 * Creates a new print object.
9268 * Parameters: 9276 * Parameters:
9269 * flags: Flags to initially configure the print object. 9277 * flags: Flags to initially configure the print object.
9270 * pages: Number of pages to print. 9278 * pages: Number of pages to print.
9273 * Returns: 9281 * Returns:
9274 * A handle to the print object or NULL on failure. 9282 * A handle to the print object or NULL on failure.
9275 */ 9283 */
9276 HPRINT API dw_print_new(unsigned long flags, unsigned int pages, void *drawfunc, void *drawdata) 9284 HPRINT API dw_print_new(unsigned long flags, unsigned int pages, void *drawfunc, void *drawdata)
9277 { 9285 {
9278 return NULL; 9286 DWPrint *print;
9279 } 9287 NSPrintPanel *panel;
9280 9288 PMPrintSettings settings;
9281 /* 9289 NSPrintInfo *pi;
9290
9291 if(!drawfunc || !(print = calloc(1, sizeof(DWPrint))))
9292 {
9293 return NULL;
9294 }
9295
9296 print->drawfunc = drawfunc;
9297 print->drawdata = drawdata;
9298 print->flags = flags;
9299
9300 /* Get the page range */
9301 pi = [NSPrintInfo sharedPrintInfo];
9302 settings = [pi PMPrintSettings];
9303 PMSetPageRange(settings, 1, pages);
9304 PMSetFirstPage(settings, 1, true);
9305 PMSetLastPage(settings, pages, true);
9306 [pi updateFromPMPrintSettings];
9307
9308 /* Create and show the print panel */
9309 panel = [NSPrintPanel printPanel];
9310 if(!panel || [panel runModalWithPrintInfo:pi] == NSCancelButton)
9311 {
9312 free(print);
9313 return NULL;
9314 }
9315 /* Put the print info from the panel into the operation */
9316 print->pi = pi;
9317
9318 return print;
9319 }
9320
9321 /*
9282 * Runs the print job, causing the draw page callbacks to fire. 9322 * Runs the print job, causing the draw page callbacks to fire.
9283 * Parameters: 9323 * Parameters:
9284 * print: Handle to the print object returned by dw_print_new(). 9324 * print: Handle to the print object returned by dw_print_new().
9285 * flags: Flags to run the print job. 9325 * flags: Flags to run the print job.
9286 * Returns: 9326 * Returns:
9287 * DW_ERROR_UNKNOWN on error or DW_ERROR_NONE on success. 9327 * DW_ERROR_UNKNOWN on error or DW_ERROR_NONE on success.
9288 */ 9328 */
9289 int API dw_print_run(HPRINT print, unsigned long flags) 9329 int API dw_print_run(HPRINT print, unsigned long flags)
9290 { 9330 {
9291 return DW_ERROR_UNKNOWN; 9331 DWPrint *p = print;
9332 NSBitmapImageRep *rep;
9333 NSPrintInfo *pi;
9334 NSPrintOperation *po;
9335 HPIXMAP pixmap;
9336 NSImage *image;
9337 NSImageView *iv;
9338 NSSize size;
9339 PMPrintSettings settings;
9340 int x;
9341 UInt32 start, end;
9342
9343 if(!p)
9344 return DW_ERROR_UNKNOWN;
9345
9346 /* Figure out the printer/paper size */
9347 pi = p->pi;
9348 size = [pi paperSize];
9349 /* Create an image view to print and a pixmap to draw into */
9350 iv = [[NSImageView alloc] init];
9351 pixmap = dw_pixmap_new(iv, size.width, size.height, 8);
9352 rep = pixmap->image;
9353
9354 /* Create an image with the data from the pixmap
9355 * to go into the image view.
9356 */
9357 image = [[NSImage alloc] initWithSize:[rep size]];
9358 [image addRepresentation:rep];
9359 [iv setImage:image];
9360
9361 /* Create the print operation using the image view and
9362 * print info obtained from the panel in the last call.
9363 */
9364 po = [NSPrintOperation printOperationWithView:iv printInfo:pi];
9365 [po setShowsPrintPanel:NO];
9366
9367 /* Get the page range */
9368 settings = [pi PMPrintSettings];
9369 PMGetFirstPage(settings, &start);
9370 if(start)
9371 start--;
9372 PMGetLastPage(settings, &end);
9373 PMSetPageRange(settings, 1, 1);
9374 PMSetFirstPage(settings, 1, true);
9375 PMSetLastPage(settings, 1, true);
9376 [pi updateFromPMPrintSettings];
9377
9378 /* Cycle through each page */
9379 for(x=start; x<end && p->drawfunc; x++)
9380 {
9381 /* Call the application's draw function */
9382 p->drawfunc(print, pixmap, x, p->drawdata);
9383 /* Print the image view */
9384 [po runOperation];
9385 /* Fill the pixmap with white in case we are printing more pages */
9386 dw_color_foreground_set(DW_CLR_WHITE);
9387 dw_draw_rect(0, pixmap, TRUE, 0, 0, (int)size.width, (int)size.height);
9388 }
9389 /* Free memory */
9390 dw_pixmap_destroy(pixmap);
9391 free(p);
9392 return p->drawfunc ? DW_ERROR_NONE : DW_ERROR_UNKNOWN;
9292 } 9393 }
9293 9394
9294 /* 9395 /*
9295 * Cancels the print job, typically called from a draw page callback. 9396 * Cancels the print job, typically called from a draw page callback.
9296 * Parameters: 9397 * Parameters:
9297 * print: Handle to the print object returned by dw_print_new(). 9398 * print: Handle to the print object returned by dw_print_new().
9298 */ 9399 */
9299 void API dw_print_cancel(HPRINT print) 9400 void API dw_print_cancel(HPRINT print)
9300 { 9401 {
9301 } 9402 DWPrint *p = print;
9302 9403
9404 if(p)
9405 p->drawfunc = NULL;
9406 }
9407