| 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 #import <iomanip> | 10 #import <iomanip> |
| 11 #import <numeric> | 11 #import <numeric> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/mac/scoped_cftyperef.h" | 14 #include "base/mac/scoped_cftyperef.h" |
| 15 #include "base/mac/scoped_nsautorelease_pool.h" | 15 #include "base/mac/scoped_nsautorelease_pool.h" |
| 16 #include "base/mac/scoped_nsexception_enabler.h" | 16 #include "base/mac/scoped_nsexception_enabler.h" |
| 17 #include "base/strings/sys_string_conversions.h" | 17 #include "base/strings/sys_string_conversions.h" |
| 18 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/values.h" | 19 #include "base/values.h" |
| 19 #include "printing/print_settings_initializer_mac.h" | 20 #include "printing/print_settings_initializer_mac.h" |
| 20 | 21 |
| 21 namespace printing { | 22 namespace printing { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // Return true if PPD name of paper is equal. | 26 // Return true if PPD name of paper is equal. |
| 26 bool IsPaperNameEqual(const PMPaper& paper1, const PMPaper& paper2) { | 27 bool IsPaperNameEqual(const PMPaper& paper1, const PMPaper& paper2) { |
| 27 CFStringRef name1 = NULL; | 28 CFStringRef name1 = NULL; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 PMPrintSettingsSetJobName(printSettings, (CFStringRef)job_title); | 91 PMPrintSettingsSetJobName(printSettings, (CFStringRef)job_title); |
| 91 [printInfo updateFromPMPrintSettings]; | 92 [printInfo updateFromPMPrintSettings]; |
| 92 } | 93 } |
| 93 } | 94 } |
| 94 | 95 |
| 95 // TODO(stuartmorgan): We really want a tab sheet here, not a modal window. | 96 // TODO(stuartmorgan): We really want a tab sheet here, not a modal window. |
| 96 // Will require restructuring the PrintingContext API to use a callback. | 97 // Will require restructuring the PrintingContext API to use a callback. |
| 97 NSInteger selection = [panel runModalWithPrintInfo:printInfo]; | 98 NSInteger selection = [panel runModalWithPrintInfo:printInfo]; |
| 98 if (selection == NSOKButton) { | 99 if (selection == NSOKButton) { |
| 99 print_info_.reset([[panel printInfo] retain]); | 100 print_info_.reset([[panel printInfo] retain]); |
| 100 InitPrintSettingsFromPrintInfo(GetPageRangesFromPrintInfo()); | 101 settings_.set_ranges(GetPageRangesFromPrintInfo()); |
| 102 InitPrintSettingsFromPrintInfo(); |
| 101 callback.Run(OK); | 103 callback.Run(OK); |
| 102 } else { | 104 } else { |
| 103 callback.Run(CANCEL); | 105 callback.Run(CANCEL); |
| 104 } | 106 } |
| 105 } | 107 } |
| 106 | 108 |
| 107 PrintingContext::Result PrintingContextMac::UseDefaultSettings() { | 109 PrintingContext::Result PrintingContextMac::UseDefaultSettings() { |
| 108 DCHECK(!in_print_job_); | 110 DCHECK(!in_print_job_); |
| 109 | 111 |
| 110 print_info_.reset([[NSPrintInfo sharedPrintInfo] copy]); | 112 print_info_.reset([[NSPrintInfo sharedPrintInfo] copy]); |
| 111 InitPrintSettingsFromPrintInfo(GetPageRangesFromPrintInfo()); | 113 settings_.set_ranges(GetPageRangesFromPrintInfo()); |
| 114 InitPrintSettingsFromPrintInfo(); |
| 112 | 115 |
| 113 return OK; | 116 return OK; |
| 114 } | 117 } |
| 115 | 118 |
| 116 PrintingContext::Result PrintingContextMac::UpdatePrinterSettings( | 119 PrintingContext::Result PrintingContextMac::UpdatePrinterSettings( |
| 117 const DictionaryValue& job_settings, const PageRanges& ranges) { | 120 bool target_is_pdf, |
| 121 bool external_preview) { |
| 118 DCHECK(!in_print_job_); | 122 DCHECK(!in_print_job_); |
| 119 | 123 |
| 120 // NOTE: Reset |print_info_| with a copy of |sharedPrintInfo| so as to start | 124 // NOTE: Reset |print_info_| with a copy of |sharedPrintInfo| so as to start |
| 121 // with a clean slate. | 125 // with a clean slate. |
| 122 print_info_.reset([[NSPrintInfo sharedPrintInfo] copy]); | 126 print_info_.reset([[NSPrintInfo sharedPrintInfo] copy]); |
| 123 | 127 |
| 124 bool collate; | 128 if (!target_is_pdf) { |
| 125 int color; | 129 if (!SetPrinter(UTF16ToUTF8(settings_.device_name())) || |
| 126 bool landscape; | 130 !SetCopiesInPrintSettings(settings_.copies()) || |
| 127 bool print_to_pdf; | 131 !SetCollateInPrintSettings(settings_.collate()) || |
| 128 bool is_cloud_dialog; | 132 !SetDuplexModeInPrintSettings(settings_.duplex_mode()) || |
| 129 int copies; | 133 !SetOutputColor(settings_.color())) { |
| 130 int duplex_mode; | 134 return OnError(); |
| 131 std::string device_name; | 135 } |
| 136 } |
| 132 | 137 |
| 133 if (!job_settings.GetBoolean(kSettingLandscape, &landscape) || | 138 if ((external_preview && !SetPrintPreviewJob()) || |
| 134 !job_settings.GetBoolean(kSettingCollate, &collate) || | 139 !UpdatePageFormatWithPaperInfo() || |
| 135 !job_settings.GetInteger(kSettingColor, &color) || | 140 !SetOrientationIsLandscape(settings_.landscape())) { |
| 136 !job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf) || | |
| 137 !job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) || | |
| 138 !job_settings.GetInteger(kSettingCopies, &copies) || | |
| 139 !job_settings.GetString(kSettingDeviceName, &device_name) || | |
| 140 !job_settings.GetBoolean(kSettingCloudPrintDialog, &is_cloud_dialog)) { | |
| 141 return OnError(); | 141 return OnError(); |
| 142 } | 142 } |
| 143 | 143 |
| 144 bool print_to_cloud = job_settings.HasKey(kSettingCloudPrintId); | |
| 145 bool open_pdf_in_preview = job_settings.HasKey(kSettingOpenPDFInPreview); | |
| 146 | |
| 147 if (!print_to_pdf && !print_to_cloud && !is_cloud_dialog) { | |
| 148 if (!SetPrinter(device_name)) | |
| 149 return OnError(); | |
| 150 | |
| 151 if (!SetCopiesInPrintSettings(copies)) | |
| 152 return OnError(); | |
| 153 | |
| 154 if (!SetCollateInPrintSettings(collate)) | |
| 155 return OnError(); | |
| 156 | |
| 157 if (!SetDuplexModeInPrintSettings( | |
| 158 static_cast<DuplexMode>(duplex_mode))) { | |
| 159 return OnError(); | |
| 160 } | |
| 161 | |
| 162 if (!SetOutputColor(color)) | |
| 163 return OnError(); | |
| 164 } | |
| 165 if (open_pdf_in_preview) { | |
| 166 if (!SetPrintPreviewJob()) | |
| 167 return OnError(); | |
| 168 } | |
| 169 | |
| 170 if (!UpdatePageFormatWithPaperInfo()) | |
| 171 return OnError(); | |
| 172 | |
| 173 if (!SetOrientationIsLandscape(landscape)) | |
| 174 return OnError(); | |
| 175 | |
| 176 [print_info_.get() updateFromPMPrintSettings]; | 144 [print_info_.get() updateFromPMPrintSettings]; |
| 177 | 145 |
| 178 InitPrintSettingsFromPrintInfo(ranges); | 146 InitPrintSettingsFromPrintInfo(); |
| 179 return OK; | 147 return OK; |
| 180 } | 148 } |
| 181 | 149 |
| 182 bool PrintingContextMac::SetPrintPreviewJob() { | 150 bool PrintingContextMac::SetPrintPreviewJob() { |
| 183 PMPrintSession print_session = | 151 PMPrintSession print_session = |
| 184 static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); | 152 static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); |
| 185 PMPrintSettings print_settings = | 153 PMPrintSettings print_settings = |
| 186 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); | 154 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); |
| 187 return PMSessionSetDestination( | 155 return PMSessionSetDestination( |
| 188 print_session, print_settings, kPMDestinationPreview, | 156 print_session, print_settings, kPMDestinationPreview, |
| 189 NULL, NULL) == noErr; | 157 NULL, NULL) == noErr; |
| 190 } | 158 } |
| 191 | 159 |
| 192 void PrintingContextMac::InitPrintSettingsFromPrintInfo( | 160 void PrintingContextMac::InitPrintSettingsFromPrintInfo() { |
| 193 const PageRanges& ranges) { | |
| 194 PMPrintSession print_session = | 161 PMPrintSession print_session = |
| 195 static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); | 162 static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); |
| 196 PMPageFormat page_format = | 163 PMPageFormat page_format = |
| 197 static_cast<PMPageFormat>([print_info_.get() PMPageFormat]); | 164 static_cast<PMPageFormat>([print_info_.get() PMPageFormat]); |
| 198 PMPrinter printer; | 165 PMPrinter printer; |
| 199 PMSessionGetCurrentPrinter(print_session, &printer); | 166 PMSessionGetCurrentPrinter(print_session, &printer); |
| 167 settings_.set_selection_only(false); |
| 200 PrintSettingsInitializerMac::InitPrintSettings( | 168 PrintSettingsInitializerMac::InitPrintSettings( |
| 201 printer, page_format, ranges, false, &settings_); | 169 printer, page_format, &settings_); |
| 202 } | 170 } |
| 203 | 171 |
| 204 bool PrintingContextMac::SetPrinter(const std::string& device_name) { | 172 bool PrintingContextMac::SetPrinter(const std::string& device_name) { |
| 205 DCHECK(print_info_.get()); | 173 DCHECK(print_info_.get()); |
| 206 PMPrintSession print_session = | 174 PMPrintSession print_session = |
| 207 static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); | 175 static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); |
| 208 | 176 |
| 209 PMPrinter current_printer; | 177 PMPrinter current_printer; |
| 210 if (PMSessionGetCurrentPrinter(print_session, ¤t_printer) != noErr) | 178 if (PMSessionGetCurrentPrinter(print_session, ¤t_printer) != noErr) |
| 211 return false; | 179 return false; |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 void PrintingContextMac::ReleaseContext() { | 478 void PrintingContextMac::ReleaseContext() { |
| 511 print_info_.reset(); | 479 print_info_.reset(); |
| 512 context_ = NULL; | 480 context_ = NULL; |
| 513 } | 481 } |
| 514 | 482 |
| 515 gfx::NativeDrawingContext PrintingContextMac::context() const { | 483 gfx::NativeDrawingContext PrintingContextMac::context() const { |
| 516 return context_; | 484 return context_; |
| 517 } | 485 } |
| 518 | 486 |
| 519 } // namespace printing | 487 } // namespace printing |
| OLD | NEW |