views:

1131

answers:

1

The AppKit release notes for Leopard say:

In many applications it is not appropriate to present page setup panels to the user, or even include a Page Setup... item in the File menu, but there has been no other easy way to let the user specify page setup parameters to be used when printing. (New UI advice: if your application doesn't persistently store page setup parameters on a per-document basis, or have some mechanism to associate them with whatever other kind of large-scale objects your application may deal with, it probably shouldn't have a page setup panel at all.)

I have fairly simple printing needs and would like to eliminate the “Page Setup...” command from my application.

My previous recipe for printing text was to create an offscreen text view of an appropriate size for the current paper size and orientation, then start a print operation for that view.

However, if allow the user to change the paper size and orientation in the print panel, I need to react to that change and reconfigure my printing text view.

What is the correct way to handle this situation?

My current hacked up sample app does this by subclassing NSTextView and reconfiguring for the new page parameters in -[MyTextView knowsPageRange:].

+1  A: 

One approach that's slightly hacky but works really well is to run two NSPrintOperations.

We use this in our app's printing code, to detect if the user selected a regular printer or a receipt printer in the print panel. The app prints an entirely different document for each kind of printer.

So in PyObjC, untested code:

def smartlyPrintView_(self, theViewToPrint):
 # set up and run an initial NSPrintOperation to get printInfo
 fakePrintOperation = NSPrintOperation.printOperationWithView_(theViewToPrint)
 NSPrintOperation.setCurrentOperation_(fakePrintOperation)

 if NSPrintPanel.printPanel().runModal() == True:
  # get the printInfo so we can do stuff with it
  printInfo = fakePrintOperation.printInfo()
  # get rid of our fakePrintOperation
  NSPrintOperation.currentOperation().cleanUpOperation()

  # do stuff

  # run a real NSPrintOperation without a printPanel
  realPrintOperation = NSPrintOperation.printOperationWithView_printInfo_(theViewToPrint, printInfo)
  realPrintOperation.setShowsPrintPanel_(False)
  realPrintOperation.runOperation()

 else:
  NSPrintOperation.currentOperation().cleanUpOperation()

Recipe to translate this to Obj-C:

Add some variable declarations, replace the underscores with colons, and interleave the arguments with the method's clauses. Mix in half a cup of flour and a handful of semi-colons. Simmer at low heat for thirthy minutes and serve hot. Good luck :)

Dirk Stoop