OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "printing/printing_context_mac.h" | 5 #include "printing/printing_context_mac.h" |
6 | 6 |
7 #import <ApplicationServices/ApplicationServices.h> | 7 #import <ApplicationServices/ApplicationServices.h> |
8 #import <AppKit/AppKit.h> | 8 #import <AppKit/AppKit.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/mac/scoped_cftyperef.h" | 11 #include "base/mac/scoped_cftyperef.h" |
12 #include "base/mac/scoped_nsautorelease_pool.h" | 12 #include "base/mac/scoped_nsautorelease_pool.h" |
13 #include "base/mac/scoped_nsexception_enabler.h" | 13 #include "base/mac/scoped_nsexception_enabler.h" |
14 #include "base/sys_string_conversions.h" | 14 #include "base/sys_string_conversions.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "printing/print_settings_initializer_mac.h" | 16 #include "printing/print_settings_initializer_mac.h" |
17 | 17 |
18 static const CFStringRef kColorModel = CFSTR("ColorModel"); | 18 static const CFStringRef kColorModel = CFSTR("ColorModel"); |
19 static const CFStringRef kGrayColor = CFSTR("Gray"); | 19 static const CFStringRef kGrayColor = CFSTR("Gray"); |
20 | 20 |
21 namespace printing { | 21 namespace printing { |
22 | 22 |
23 // static | 23 // static |
24 PrintingContext* PrintingContext::Create(const std::string& app_locale) { | 24 PrintingContext* PrintingContext::Create(const std::string& app_locale) { |
25 return static_cast<PrintingContext*>(new PrintingContextMac(app_locale)); | 25 return static_cast<PrintingContext*>(new PrintingContextMac(app_locale)); |
26 } | 26 } |
27 | 27 |
28 PrintingContextMac::PrintingContextMac(const std::string& app_locale) | 28 PrintingContextMac::PrintingContextMac(const std::string& app_locale) |
29 : PrintingContext(app_locale), | 29 : PrintingContext(app_locale), |
| 30 print_info_([[NSPrintInfo sharedPrintInfo] copy]), |
30 context_(NULL) { | 31 context_(NULL) { |
31 } | 32 } |
32 | 33 |
33 PrintingContextMac::~PrintingContextMac() { | 34 PrintingContextMac::~PrintingContextMac() { |
34 ReleaseContext(); | 35 ReleaseContext(); |
35 } | 36 } |
36 | 37 |
37 void PrintingContextMac::AskUserForSettings(gfx::NativeView parent_view, | 38 void PrintingContextMac::AskUserForSettings(gfx::NativeView parent_view, |
38 int max_pages, | 39 int max_pages, |
39 bool has_selection, | 40 bool has_selection, |
(...skipping 10 matching lines...) Expand all Loading... |
50 DCHECK([NSThread isMainThread]); | 51 DCHECK([NSThread isMainThread]); |
51 | 52 |
52 // We deliberately don't feed max_pages into the dialog, because setting | 53 // We deliberately don't feed max_pages into the dialog, because setting |
53 // NSPrintLastPage makes the print dialog pre-select the option to only print | 54 // NSPrintLastPage makes the print dialog pre-select the option to only print |
54 // a range. | 55 // a range. |
55 | 56 |
56 // TODO(stuartmorgan): implement 'print selection only' (probably requires | 57 // TODO(stuartmorgan): implement 'print selection only' (probably requires |
57 // adding a new custom view to the panel on 10.5; 10.6 has | 58 // adding a new custom view to the panel on 10.5; 10.6 has |
58 // NSPrintPanelShowsPrintSelection). | 59 // NSPrintPanelShowsPrintSelection). |
59 NSPrintPanel* panel = [NSPrintPanel printPanel]; | 60 NSPrintPanel* panel = [NSPrintPanel printPanel]; |
60 NSPrintInfo* printInfo = [NSPrintInfo sharedPrintInfo]; | 61 NSPrintInfo* printInfo = print_info_.get(); |
61 | 62 |
62 NSPrintPanelOptions options = [panel options]; | 63 NSPrintPanelOptions options = [panel options]; |
63 options |= NSPrintPanelShowsPaperSize; | 64 options |= NSPrintPanelShowsPaperSize; |
64 options |= NSPrintPanelShowsOrientation; | 65 options |= NSPrintPanelShowsOrientation; |
65 options |= NSPrintPanelShowsScaling; | 66 options |= NSPrintPanelShowsScaling; |
66 [panel setOptions:options]; | 67 [panel setOptions:options]; |
67 | 68 |
68 // Set the print job title text. | 69 // Set the print job title text. |
69 if (parent_view) { | 70 if (parent_view) { |
70 NSString* job_title = [[parent_view window] title]; | 71 NSString* job_title = [[parent_view window] title]; |
71 if (job_title) { | 72 if (job_title) { |
72 PMPrintSettings printSettings = | 73 PMPrintSettings printSettings = |
73 (PMPrintSettings)[printInfo PMPrintSettings]; | 74 (PMPrintSettings)[printInfo PMPrintSettings]; |
74 PMPrintSettingsSetJobName(printSettings, (CFStringRef)job_title); | 75 PMPrintSettingsSetJobName(printSettings, (CFStringRef)job_title); |
75 [printInfo updateFromPMPrintSettings]; | 76 [printInfo updateFromPMPrintSettings]; |
76 } | 77 } |
77 } | 78 } |
78 | 79 |
79 // TODO(stuartmorgan): We really want a tab sheet here, not a modal window. | 80 // TODO(stuartmorgan): We really want a tab sheet here, not a modal window. |
80 // Will require restructuring the PrintingContext API to use a callback. | 81 // Will require restructuring the PrintingContext API to use a callback. |
81 NSInteger selection = [panel runModalWithPrintInfo:printInfo]; | 82 NSInteger selection = [panel runModalWithPrintInfo:printInfo]; |
82 if (selection == NSOKButton) { | 83 if (selection == NSOKButton) { |
83 ParsePrintInfo([panel printInfo]); | 84 print_info_.reset([[panel printInfo] retain]); |
| 85 InitPrintSettingsFromPrintInfo(GetPageRangesFromPrintInfo()); |
84 callback->Run(OK); | 86 callback->Run(OK); |
85 } else { | 87 } else { |
86 callback->Run(CANCEL); | 88 callback->Run(CANCEL); |
87 } | 89 } |
88 } | 90 } |
89 | 91 |
90 PrintingContext::Result PrintingContextMac::UseDefaultSettings() { | 92 PrintingContext::Result PrintingContextMac::UseDefaultSettings() { |
91 DCHECK(!in_print_job_); | 93 DCHECK(!in_print_job_); |
92 | 94 |
93 ParsePrintInfo([NSPrintInfo sharedPrintInfo]); | 95 print_info_.reset([[NSPrintInfo sharedPrintInfo] copy]); |
| 96 InitPrintSettingsFromPrintInfo(GetPageRangesFromPrintInfo()); |
94 | 97 |
95 return OK; | 98 return OK; |
96 } | 99 } |
97 | 100 |
98 PrintingContext::Result PrintingContextMac::UpdatePrintSettings( | 101 PrintingContext::Result PrintingContextMac::UpdatePrintSettings( |
99 const DictionaryValue& job_settings, const PageRanges& ranges) { | 102 const DictionaryValue& job_settings, const PageRanges& ranges) { |
100 DCHECK(!in_print_job_); | 103 DCHECK(!in_print_job_); |
101 | 104 |
102 ResetSettings(); | 105 // NOTE: Reset |print_info_| with a copy of |sharedPrintInfo| so as to start |
| 106 // with a clean slate. |
103 print_info_.reset([[NSPrintInfo sharedPrintInfo] copy]); | 107 print_info_.reset([[NSPrintInfo sharedPrintInfo] copy]); |
104 | 108 |
105 bool collate; | 109 bool collate; |
106 bool color; | 110 bool color; |
107 bool landscape; | 111 bool landscape; |
108 bool print_to_pdf; | 112 bool print_to_pdf; |
109 int copies; | 113 int copies; |
110 int duplex_mode; | 114 int duplex_mode; |
111 std::string device_name; | 115 std::string device_name; |
112 | 116 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 PMPrintSettings pmPrintSettings = | 247 PMPrintSettings pmPrintSettings = |
244 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); | 248 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); |
245 CFStringRef output_color = color ? NULL : kGrayColor; | 249 CFStringRef output_color = color ? NULL : kGrayColor; |
246 | 250 |
247 return PMPrintSettingsSetValue(pmPrintSettings, | 251 return PMPrintSettingsSetValue(pmPrintSettings, |
248 kColorModel, | 252 kColorModel, |
249 output_color, | 253 output_color, |
250 false) == noErr; | 254 false) == noErr; |
251 } | 255 } |
252 | 256 |
253 void PrintingContextMac::ParsePrintInfo(NSPrintInfo* print_info) { | 257 PageRanges PrintingContextMac::GetPageRangesFromPrintInfo() { |
254 ResetSettings(); | |
255 print_info_.reset([print_info retain]); | |
256 PageRanges page_ranges; | 258 PageRanges page_ranges; |
257 NSDictionary* print_info_dict = [print_info_.get() dictionary]; | 259 NSDictionary* print_info_dict = [print_info_.get() dictionary]; |
258 if (![[print_info_dict objectForKey:NSPrintAllPages] boolValue]) { | 260 if (![[print_info_dict objectForKey:NSPrintAllPages] boolValue]) { |
259 PageRange range; | 261 PageRange range; |
260 range.from = [[print_info_dict objectForKey:NSPrintFirstPage] intValue] - 1; | 262 range.from = [[print_info_dict objectForKey:NSPrintFirstPage] intValue] - 1; |
261 range.to = [[print_info_dict objectForKey:NSPrintLastPage] intValue] - 1; | 263 range.to = [[print_info_dict objectForKey:NSPrintLastPage] intValue] - 1; |
262 page_ranges.push_back(range); | 264 page_ranges.push_back(range); |
263 } | 265 } |
264 InitPrintSettingsFromPrintInfo(page_ranges); | 266 return page_ranges; |
265 } | 267 } |
266 | 268 |
267 PrintingContext::Result PrintingContextMac::InitWithSettings( | 269 PrintingContext::Result PrintingContextMac::InitWithSettings( |
268 const PrintSettings& settings) { | 270 const PrintSettings& settings) { |
269 DCHECK(!in_print_job_); | 271 DCHECK(!in_print_job_); |
270 | 272 |
271 settings_ = settings; | 273 settings_ = settings; |
272 | 274 |
273 NOTIMPLEMENTED(); | 275 NOTIMPLEMENTED(); |
274 | 276 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 void PrintingContextMac::ReleaseContext() { | 368 void PrintingContextMac::ReleaseContext() { |
367 print_info_.reset(); | 369 print_info_.reset(); |
368 context_ = NULL; | 370 context_ = NULL; |
369 } | 371 } |
370 | 372 |
371 gfx::NativeDrawingContext PrintingContextMac::context() const { | 373 gfx::NativeDrawingContext PrintingContextMac::context() const { |
372 return context_; | 374 return context_; |
373 } | 375 } |
374 | 376 |
375 } // namespace printing | 377 } // namespace printing |
OLD | NEW |