-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSLBPrintPanel.m
More file actions
64 lines (54 loc) · 2.86 KB
/
Copy pathSLBPrintPanel.m
File metadata and controls
64 lines (54 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// SLBPrintPanel.m
// ScratchLabels
//
// Created by Andy Lee on 9/23/20.
//
#import "SLBPrintPanel.h"
#import <ApplicationServices/ApplicationServices.h>
@implementation SLBPrintPanel
///// Useful for debugging. Returns the printInfo's duplex mode, which is either nil (meaning no duplex mode has been set)
///// or the NSNumber equivalent of a PMDuplexMode. A value of 1 means single-sided printing, 2 means double-sided.
///// Other values are possible -- see the docs for PMDuplexMode.
//static NSNumber *GetDuplexMode(NSPrintInfo *printInfo) {
// return printInfo.printSettings[@"com_apple_print_PrintSettings_PMDuplexing"];
//}
/// Sets printInfo's duplex mode to kPMDuplexNone, meaning single-sided printing. Returns noErr if successful.
static OSStatus SetDuplexModeToSingleSided(NSPrintInfo *printInfo) {
PMPrintSettings pmPrintSettings = printInfo.PMPrintSettings;
OSStatus result = PMSetDuplex(pmPrintSettings, kPMDuplexNone);
if (result == noErr) {
[printInfo updateFromPMPrintSettings];
} else {
NSLog(@"+++ WARNING: Failed to make print info <%p> single-sided -- %d", printInfo, result);
}
return result;
}
#pragma mark - NSPrintPanel methods
+ (NSPrintPanel *)printPanel {
NSPrintPanel *printPanel = [super printPanel];
printPanel.options = (/*NSPrintPanelShowsCopies |*/ NSPrintPanelShowsPageRange | NSPrintPanelShowsPreview);
return printPanel;
}
- (void)beginSheetWithPrintInfo:(NSPrintInfo *)incomingPrintInfo modalForWindow:(NSWindow *)docWindow delegate:(nullable id)delegate didEndSelector:(nullable SEL)didEndSelector contextInfo:(nullable void *)contextInfo {
// Open the sheet by calling super. In macOS as of this writing, this call sets the
// print info's duplex mode to double-sided, no matter what the user previously chose.
//
// NOTE: The Big Sur beta as of 2020-09-29 seems to remember the previous value the
// user set in the print panel, and forces *that* to be the duplex mode. While this
// is an improvement, what we want is to force single-sided, and this kludge is still
// the only way I know to do it.
[super beginSheetWithPrintInfo:incomingPrintInfo modalForWindow:docWindow delegate:delegate didEndSelector:didEndSelector contextInfo:contextInfo];
// Hack around this by immediately changing the print info to single-sided. This does
// not update the print panel's UI -- the check box still shows double-sided -- but
// for purposes of this app I don't want to show that check box anyway, so I hide it
// by excluding NSPrintPanelShowsCopies in the override of +printPanel. This means
// the number-of-copies text field is also hidden, but again that happens to be what I
// want for this app.
OSStatus errorCode = SetDuplexModeToSingleSided(self.printInfo);
if (errorCode != noErr) {
NSLog(@"+++ ERROR: Failed to make the print panel's NSPrintInfo <%p> single-sided -- %d", self.printInfo, errorCode);
return;
}
}
@end