1 unit cocoaprndelegate;
2 
3 {$mode objfpc}{$H+}
4 {$modeswitch objectivec1}
5 
6 interface
7 
8 uses
9   Classes, SysUtils, Math, MacOSAll, CocoaAll, OSPrinters, Printers, Dialogs,
10   PrintersDlgs;
11 
12 type
13 
14   { PrintDialogDelegate }
15 
16   PrintDialogDelegate = objcclass(NSObject)
17   private
18     _collate: boolean;
19     _copies, _firstPage, _lastPage: UInt32;
20     procedure RawPrintJobDidRun(panel: NSPrintPanel; returnCode:NSInteger; contextInfo:Pointer); message 'RawPrintJobDidRun:returnCode:contextInfo:';
21     procedure PrintJobDidRun(op:NSPrintOperation; success:boolean; contextInfo:Pointer); message 'PrintJobDidRun:success:contextInfo:';
22     procedure SetupPrinterDidRun(panel: NSPrintPanel; returnCode:NSInteger; contextInfo:Pointer); message 'SetupPrinterDidRun:returnCode:contextInfo:';
23     procedure UpdatePrinter(checkList:Boolean; returnCode:NSInteger); message 'UpdatePrinter:returnCode:';
24     procedure BackupPrintSettings; message 'BackupPrintSettings';
25     procedure PrintSettingsFromDialog; message 'PrintSettingsFromDialog';
26     procedure PrintSettingsToDialog; message 'PrintSettingsToDialog';
27     procedure RestorePrintSettings; message 'RestorePrintSettings';
28   public
29     response: TDialogResultEvent;
30     attachToWindow: NSWindow;
31     renderView: NSView;
32     sender: TObject;
33     printDialog: TPrintDialog;
34     onStartJob: TNotifyEvent;
35 
RunSetupPrinternull36     function RunSetupPrinter: boolean; message 'RunSetupPrinter';
RunPrintJobnull37     function RunPrintJob: boolean; message 'RunPrintJob';
38   end;
39 
40 var
41   printDelegate: PrintDialogDelegate;
42 
43 implementation
44 
45 { PrintDialogDelegate }
46 
47 procedure PrintDialogDelegate.SetupPrinterDidRun(panel: NSPrintPanel;
48   returnCode: NSInteger; contextInfo: Pointer);
49 begin
50   UpdatePrinter(true, returnCode);
51   if Assigned(response) then
52     response(sender, returnCode=NSOKButton);
53   self.release;
54 end;
55 
PrintDialogDelegate.RunSetupPrinternull56 function PrintDialogDelegate.RunSetupPrinter: boolean;
57 var
58   PrintPanel: NSPrintPanel;
59   pInfo: NSPrintInfo;
60   res: NSInteger;
61 begin
62   PrintPanel := NSPrintPanel.printPanel;
63   //PrintPanel.setJobStyleHint(NSPrintNoPresetsJobStyleHint);
64   PrintPanel.setOptions((PrintPanel.options or NSPrintPanelShowsPaperSize) and not NSPrintPanelShowsPageRange);
65   PrintPanel.setDefaultButtonTitle(NSSTR('OK'));
66   pInfo := NSPrintInfo.sharedPrintInfo;
67 
68   if Assigned(response) and (attachToWindow<>nil) then
69   begin
70     PrintPanel.beginSheetWithPrintInfo_modalForWindow_delegate_didEndSelector_contextInfo(
71       pInfo,
72       attachToWindow,
73       self,
74       objcselector('SetupPrinterDidRun:returnCode:contextInfo:'),
75       nil
76       );
77     result := true;
78   end else
79   begin
80     res := PrintPanel.runModalWithPrintInfo(pInfo);
81     UpdatePrinter(true, res);
82     self.release;
83   end;
84 end;
85 
86 procedure PrintDialogDelegate.PrintJobDidRun(op: NSPrintOperation;
87   success: boolean; contextInfo: Pointer);
88 begin
89   if not success then
90     RestorePrintSettings
91   else
92     PrintSettingsToDialog;
93 
94   if success and assigned(onStartJob) then
95     OnStartJob(sender);
96 
97   if assigned(response) then
98     response(sender, success);
99 
100   self.release;
101 end;
102 
103 procedure PrintDialogDelegate.RawPrintJobDidRun(panel: NSPrintPanel;
104   returnCode: NSInteger; contextInfo: Pointer);
105 var
106   success: boolean;
107 begin
108   success := (returnCode=NSOKButton);
109   PrintJobDidRun(nil, success, nil);
110 end;
111 
PrintDialogDelegate.RunPrintJobnull112 function PrintDialogDelegate.RunPrintJob: boolean;
113 var
114   pInfo: NSPrintInfo;
115   printOp: NSPrintOperation;
116   printPanel: NSPrintPanel;
117   res: NSInteger;
118 begin
119   pInfo := NSPrintInfo.sharedPrintInfo;
120 
121   BackupPrintSettings;
122   PrintSettingsFromDialog;
123 
124   if renderView=nil then
125   begin
126     printPanel := NSPrintPanel.printPanel;
127     if assigned(response) and (attachToWindow<>nil) then
128     begin
129       result := true;
130       printPanel.beginSheetWithPrintInfo_modalForWindow_delegate_didEndSelector_contextInfo(
131         pInfo,
132         attachToWindow,
133         self,
134         ObjCSelector('RawPrintJobDidRun:returnCode:contextInfo:'),
135         nil
136       );
137     end else
138     begin
139       res := printPanel.runModalWithPrintInfo(pInfo);
140       result := (res=NSOKButton);
141       PrintJobDidRun(nil, result, nil);
142     end;
143   end else
144   begin
145     printOp := NSPrintOperation.printOperationWithView_printInfo(renderView, pInfo);
146     if Assigned(response) and (attachToWindow<>nil) then
147     begin
148       result := true;
149       printOp.runOperationModalForWindow_delegate_didRunSelector_contextInfo(
150         attachToWindow,
151         self,
152         objcselector('PrintJobDidRun:success:contextInfo:'),
153         nil
154       );
155     end else
156     begin
157       result := printOP.runOperation;
158       PrintJobDidRun(printOp, result, nil);
159     end;
160   end;
161 
162 end;
163 
164 procedure PrintDialogDelegate.UpdatePrinter(checkList:Boolean; returnCode: NSInteger);
165 var
166   CocoaPrinter: TCocoaPrinter;
167 begin
168   CocoaPrinter := Printer as TCocoaPrinter;
169   if checkList then
170     CocoaPrinter.CheckPrinterList;
171   if returnCode=NSOKButton then
172     CocoaPrinter.UpdatePrinter;
173 end;
174 
175 procedure PrintDialogDelegate.BackupPrintSettings;
176 var
177   pInfo: NSPrintInfo;
178 begin
179   pInfo := NSPrintInfo.sharedPrintInfo;
180 
181   _Collate := false;
182   _Copies := 1;
183   _firstPage := 1;
184   _lastPage := 1;
185   PMGetCollate(pInfo.PMPrintSettings, _Collate);
186   PMGetCopies(pInfo.PMPrintSettings, _Copies);
187   PMGetFirstPage(pInfo.PMPrintSettings, _firstPage);
188   PMGetLastPage(pInfo.PMPrintSettings, _lastPage);
189 end;
190 
191 procedure PrintDialogDelegate.PrintSettingsFromDialog;
192 var
193   PMin, PMax, PFrom, PTo: Integer;
194   pInfo: NSPrintInfo;
195   s: string;
196 begin
197   pInfo := NSPrintInfo.sharedPrintInfo;
198 
199   PMSetCollate(pInfo.PMPrintSettings, printDialog.Collate);
200   PMSetCopies(pInfo.PMPrintSettings, printDialog.Copies, False);
201 
202   PMin := printDialog.MinPage;
203   PMax := Math.Max(PMin, printDialog.MaxPage);
204   PFrom := Math.Min(Math.Max(printDialog.FromPage, PMin), PMax);
205   PTo := Max(PFrom, Min(printDialog.ToPage, PMax));
206   PMSetPageRange(pInfo.PMPrintSettings, PMin, PMax);
207 
208   if printDialog.PrintRange <> prAllPages then
209   begin
210     PMSetFirstPage(pInfo.PMPrintSettings, PFrom, False);
211     PMSetLastPage(pInfo.PMPrintSettings, PTo, False);
212   end;
213   // TODO: PrintToFile
214 
215   pInfo.updateFromPMPrintSettings;
216 end;
217 
218 procedure PrintDialogDelegate.PrintSettingsToDialog;
219 var
220   pInfo: NSPrintInfo;
221   Collate: Boolean;
222   Copies, firstPage, lastPage: UInt32;
223 begin
224   pInfo := NSPrintInfo.sharedPrintInfo;
225 
226   PMGetCollate(pInfo.PMPrintSettings, Collate);
227   PMGetCopies(pInfo.PMPrintSettings, Copies);
228   PMGetFirstPage(pInfo.PMPrintSettings, firstPage);
229   PMGetLastPage(pInfo.PMPrintSettings, lastPage);
230 
231   if lastPage>$FFFF then
232   begin
233     printDialog.PrintRange := prAllPages;
234     printDialog.FromPage := printDialog.MinPage;
235     printDialog.ToPage := printDialog.MaxPage;
236   end else
237   begin
238     printDialog.FromPage := firstPage;
239     printDialog.ToPage := lastPage;
240   end;
241   printDialog.Collate := Collate;
242   printDialog.Copies := Copies;
243   //TODO: PrintToFile
244 end;
245 
246 procedure PrintDialogDelegate.RestorePrintSettings;
247 var
248   pInfo: NSPrintInfo;
249 begin
250   pInfo := NSPrintInfo.sharedPrintInfo;
251 
252   PMSetCollate(pInfo.PMPrintSettings, _Collate);
253   PMSetCopies(pInfo.PMPrintSettings, _Copies, False);
254   PMSetFirstPage(pInfo.PMPrintSettings, _firstPage, False);
255   PMSetLastPage(pInfo.PMPrintSettings, _lastPage, False);
256 
257   pInfo.updateFromPMPrintSettings;
258 end;
259 
260 
261 
262 end.
263 
264