| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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.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/scoped_cftyperef.h" |
| 11 #include "base/sys_string_conversions.h" | 12 #include "base/sys_string_conversions.h" |
| 12 | 13 |
| 13 namespace printing { | 14 namespace printing { |
| 14 | 15 |
| 15 PrintingContext::PrintingContext() | 16 // static |
| 16 : context_(NULL), | 17 PrintingContext* PrintingContext::Create() { |
| 17 print_info_(nil), | 18 return static_cast<PrintingContext*>(new PrintingContextMac); |
| 18 dialog_box_dismissed_(false), | |
| 19 in_print_job_(false), | |
| 20 abort_printing_(false) { | |
| 21 } | 19 } |
| 22 | 20 |
| 23 PrintingContext::~PrintingContext() { | 21 PrintingContextMac::PrintingContextMac() |
| 24 ResetSettings(); | 22 : PrintingContext(), |
| 23 print_info_(NULL), |
| 24 context_(NULL) { |
| 25 } | 25 } |
| 26 | 26 |
| 27 PrintingContextMac::~PrintingContextMac() { |
| 28 ReleaseContext(); |
| 29 } |
| 27 | 30 |
| 28 void PrintingContext::AskUserForSettings(gfx::NativeView parent_view, | 31 void PrintingContextMac::AskUserForSettings(gfx::NativeView parent_view, |
| 29 int max_pages, | 32 int max_pages, |
| 30 bool has_selection, | 33 bool has_selection, |
| 31 PrintSettingsCallback* callback) { | 34 PrintSettingsCallback* callback) { |
| 32 DCHECK([NSThread isMainThread]); | 35 DCHECK([NSThread isMainThread]); |
| 33 | 36 |
| 34 // We deliberately don't feed max_pages into the dialog, because setting | 37 // We deliberately don't feed max_pages into the dialog, because setting |
| 35 // NSPrintLastPage makes the print dialog pre-select the option to only print | 38 // NSPrintLastPage makes the print dialog pre-select the option to only print |
| 36 // a range. | 39 // a range. |
| 37 | 40 |
| 38 // TODO(stuartmorgan): implement 'print selection only' (probably requires | 41 // TODO(stuartmorgan): implement 'print selection only' (probably requires |
| 39 // adding a new custom view to the panel on 10.5; 10.6 has | 42 // adding a new custom view to the panel on 10.5; 10.6 has |
| 40 // NSPrintPanelShowsPrintSelection). | 43 // NSPrintPanelShowsPrintSelection). |
| 41 NSPrintPanel* panel = [NSPrintPanel printPanel]; | 44 NSPrintPanel* panel = [NSPrintPanel printPanel]; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 61 // Will require restructuring the PrintingContext API to use a callback. | 64 // Will require restructuring the PrintingContext API to use a callback. |
| 62 NSInteger selection = [panel runModalWithPrintInfo:printInfo]; | 65 NSInteger selection = [panel runModalWithPrintInfo:printInfo]; |
| 63 if (selection == NSOKButton) { | 66 if (selection == NSOKButton) { |
| 64 ParsePrintInfo([panel printInfo]); | 67 ParsePrintInfo([panel printInfo]); |
| 65 callback->Run(OK); | 68 callback->Run(OK); |
| 66 } else { | 69 } else { |
| 67 callback->Run(CANCEL); | 70 callback->Run(CANCEL); |
| 68 } | 71 } |
| 69 } | 72 } |
| 70 | 73 |
| 71 PrintingContext::Result PrintingContext::UseDefaultSettings() { | 74 PrintingContext::Result PrintingContextMac::UseDefaultSettings() { |
| 72 DCHECK(!in_print_job_); | 75 DCHECK(!in_print_job_); |
| 73 | 76 |
| 74 ParsePrintInfo([NSPrintInfo sharedPrintInfo]); | 77 ParsePrintInfo([NSPrintInfo sharedPrintInfo]); |
| 75 | 78 |
| 76 return OK; | 79 return OK; |
| 77 } | 80 } |
| 78 | 81 |
| 79 void PrintingContext::ParsePrintInfo(NSPrintInfo* print_info) { | 82 void PrintingContextMac::ParsePrintInfo(NSPrintInfo* print_info) { |
| 80 ResetSettings(); | 83 ResetSettings(); |
| 81 print_info_ = [print_info retain]; | 84 print_info_ = [print_info retain]; |
| 82 PageRanges page_ranges; | 85 PageRanges page_ranges; |
| 83 NSDictionary* print_info_dict = [print_info_ dictionary]; | 86 NSDictionary* print_info_dict = [print_info_ dictionary]; |
| 84 if (![[print_info_dict objectForKey:NSPrintAllPages] boolValue]) { | 87 if (![[print_info_dict objectForKey:NSPrintAllPages] boolValue]) { |
| 85 PageRange range; | 88 PageRange range; |
| 86 range.from = [[print_info_dict objectForKey:NSPrintFirstPage] intValue] - 1; | 89 range.from = [[print_info_dict objectForKey:NSPrintFirstPage] intValue] - 1; |
| 87 range.to = [[print_info_dict objectForKey:NSPrintLastPage] intValue] - 1; | 90 range.to = [[print_info_dict objectForKey:NSPrintLastPage] intValue] - 1; |
| 88 page_ranges.push_back(range); | 91 page_ranges.push_back(range); |
| 89 } | 92 } |
| 90 PMPrintSession print_session = | 93 PMPrintSession print_session = |
| 91 static_cast<PMPrintSession>([print_info_ PMPrintSession]); | 94 static_cast<PMPrintSession>([print_info_ PMPrintSession]); |
| 92 PMPageFormat page_format = | 95 PMPageFormat page_format = |
| 93 static_cast<PMPageFormat>([print_info_ PMPageFormat]); | 96 static_cast<PMPageFormat>([print_info_ PMPageFormat]); |
| 94 PMPrinter printer; | 97 PMPrinter printer; |
| 95 PMSessionGetCurrentPrinter(print_session, &printer); | 98 PMSessionGetCurrentPrinter(print_session, &printer); |
| 96 | 99 |
| 97 settings_.Init(printer, page_format, page_ranges, false); | 100 settings_.Init(printer, page_format, page_ranges, false); |
| 98 } | 101 } |
| 99 | 102 |
| 100 PrintingContext::Result PrintingContext::InitWithSettings( | 103 PrintingContext::Result PrintingContextMac::InitWithSettings( |
| 101 const PrintSettings& settings) { | 104 const PrintSettings& settings) { |
| 102 DCHECK(!in_print_job_); | 105 DCHECK(!in_print_job_); |
| 103 settings_ = settings; | 106 settings_ = settings; |
| 104 | 107 |
| 105 NOTIMPLEMENTED(); | 108 NOTIMPLEMENTED(); |
| 106 | 109 |
| 107 return FAILED; | 110 return FAILED; |
| 108 } | 111 } |
| 109 | 112 |
| 110 void PrintingContext::ResetSettings() { | 113 PrintingContext::Result PrintingContextMac::NewDocument( |
| 111 [print_info_ autorelease]; | |
| 112 print_info_ = nil; | |
| 113 settings_.Clear(); | |
| 114 dialog_box_dismissed_ = false; | |
| 115 abort_printing_ = false; | |
| 116 in_print_job_ = false; | |
| 117 context_ = NULL; | |
| 118 } | |
| 119 | |
| 120 PrintingContext::Result PrintingContext::NewDocument( | |
| 121 const string16& document_name) { | 114 const string16& document_name) { |
| 122 DCHECK(!in_print_job_); | 115 DCHECK(!in_print_job_); |
| 123 | 116 |
| 124 in_print_job_ = true; | 117 in_print_job_ = true; |
| 125 | 118 |
| 126 PMPrintSession print_session = | 119 PMPrintSession print_session = |
| 127 static_cast<PMPrintSession>([print_info_ PMPrintSession]); | 120 static_cast<PMPrintSession>([print_info_ PMPrintSession]); |
| 128 PMPrintSettings print_settings = | 121 PMPrintSettings print_settings = |
| 129 static_cast<PMPrintSettings>([print_info_ PMPrintSettings]); | 122 static_cast<PMPrintSettings>([print_info_ PMPrintSettings]); |
| 130 PMPageFormat page_format = | 123 PMPageFormat page_format = |
| 131 static_cast<PMPageFormat>([print_info_ PMPageFormat]); | 124 static_cast<PMPageFormat>([print_info_ PMPageFormat]); |
| 132 | 125 |
| 133 scoped_cftyperef<CFStringRef> job_title( | 126 scoped_cftyperef<CFStringRef> job_title( |
| 134 base::SysUTF16ToCFStringRef(document_name)); | 127 base::SysUTF16ToCFStringRef(document_name)); |
| 135 PMPrintSettingsSetJobName(print_settings, job_title.get()); | 128 PMPrintSettingsSetJobName(print_settings, job_title.get()); |
| 136 | 129 |
| 137 OSStatus status = PMSessionBeginCGDocumentNoDialog(print_session, | 130 OSStatus status = PMSessionBeginCGDocumentNoDialog(print_session, |
| 138 print_settings, | 131 print_settings, |
| 139 page_format); | 132 page_format); |
| 140 if (status != noErr) | 133 if (status != noErr) |
| 141 return OnError(); | 134 return OnError(); |
| 142 | 135 |
| 143 return OK; | 136 return OK; |
| 144 } | 137 } |
| 145 | 138 |
| 146 PrintingContext::Result PrintingContext::NewPage() { | 139 PrintingContext::Result PrintingContextMac::NewPage() { |
| 147 if (abort_printing_) | 140 if (abort_printing_) |
| 148 return CANCEL; | 141 return CANCEL; |
| 149 DCHECK(in_print_job_); | 142 DCHECK(in_print_job_); |
| 150 DCHECK(!context_); | 143 DCHECK(!context_); |
| 151 | 144 |
| 152 PMPrintSession print_session = | 145 PMPrintSession print_session = |
| 153 static_cast<PMPrintSession>([print_info_ PMPrintSession]); | 146 static_cast<PMPrintSession>([print_info_ PMPrintSession]); |
| 154 PMPageFormat page_format = | 147 PMPageFormat page_format = |
| 155 static_cast<PMPageFormat>([print_info_ PMPageFormat]); | 148 static_cast<PMPageFormat>([print_info_ PMPageFormat]); |
| 156 OSStatus status; | 149 OSStatus status; |
| 157 status = PMSessionBeginPageNoDialog(print_session, page_format, NULL); | 150 status = PMSessionBeginPageNoDialog(print_session, page_format, NULL); |
| 158 if (status != noErr) | 151 if (status != noErr) |
| 159 return OnError(); | 152 return OnError(); |
| 160 status = PMSessionGetCGGraphicsContext(print_session, &context_); | 153 status = PMSessionGetCGGraphicsContext(print_session, &context_); |
| 161 if (status != noErr) | 154 if (status != noErr) |
| 162 return OnError(); | 155 return OnError(); |
| 163 | 156 |
| 164 return OK; | 157 return OK; |
| 165 } | 158 } |
| 166 | 159 |
| 167 PrintingContext::Result PrintingContext::PageDone() { | 160 PrintingContext::Result PrintingContextMac::PageDone() { |
| 168 if (abort_printing_) | 161 if (abort_printing_) |
| 169 return CANCEL; | 162 return CANCEL; |
| 170 DCHECK(in_print_job_); | 163 DCHECK(in_print_job_); |
| 171 DCHECK(context_); | 164 DCHECK(context_); |
| 172 | 165 |
| 173 PMPrintSession print_session = | 166 PMPrintSession print_session = |
| 174 static_cast<PMPrintSession>([print_info_ PMPrintSession]); | 167 static_cast<PMPrintSession>([print_info_ PMPrintSession]); |
| 175 OSStatus status = PMSessionEndPageNoDialog(print_session); | 168 OSStatus status = PMSessionEndPageNoDialog(print_session); |
| 176 if (status != noErr) | 169 if (status != noErr) |
| 177 OnError(); | 170 OnError(); |
| 178 context_ = NULL; | 171 context_ = NULL; |
| 179 | 172 |
| 180 return OK; | 173 return OK; |
| 181 } | 174 } |
| 182 | 175 |
| 183 PrintingContext::Result PrintingContext::DocumentDone() { | 176 PrintingContext::Result PrintingContextMac::DocumentDone() { |
| 184 if (abort_printing_) | 177 if (abort_printing_) |
| 185 return CANCEL; | 178 return CANCEL; |
| 186 DCHECK(in_print_job_); | 179 DCHECK(in_print_job_); |
| 187 | 180 |
| 188 PMPrintSession print_session = | 181 PMPrintSession print_session = |
| 189 static_cast<PMPrintSession>([print_info_ PMPrintSession]); | 182 static_cast<PMPrintSession>([print_info_ PMPrintSession]); |
| 190 OSStatus status = PMSessionEndDocumentNoDialog(print_session); | 183 OSStatus status = PMSessionEndDocumentNoDialog(print_session); |
| 191 if (status != noErr) | 184 if (status != noErr) |
| 192 OnError(); | 185 OnError(); |
| 193 | 186 |
| 194 ResetSettings(); | 187 ResetSettings(); |
| 195 return OK; | 188 return OK; |
| 196 } | 189 } |
| 197 | 190 |
| 198 void PrintingContext::Cancel() { | 191 void PrintingContextMac::Cancel() { |
| 199 abort_printing_ = true; | 192 abort_printing_ = true; |
| 200 in_print_job_ = false; | 193 in_print_job_ = false; |
| 201 context_ = NULL; | 194 context_ = NULL; |
| 202 | 195 |
| 203 PMPrintSession print_session = | 196 PMPrintSession print_session = |
| 204 static_cast<PMPrintSession>([print_info_ PMPrintSession]); | 197 static_cast<PMPrintSession>([print_info_ PMPrintSession]); |
| 205 PMSessionEndPageNoDialog(print_session); | 198 PMSessionEndPageNoDialog(print_session); |
| 206 } | 199 } |
| 207 | 200 |
| 208 void PrintingContext::DismissDialog() { | 201 void PrintingContextMac::DismissDialog() { |
| 209 NOTIMPLEMENTED(); | 202 NOTIMPLEMENTED(); |
| 210 } | 203 } |
| 211 | 204 |
| 212 PrintingContext::Result PrintingContext::OnError() { | 205 void PrintingContextMac::ReleaseContext() { |
| 213 ResetSettings(); | 206 if (print_info_) { |
| 214 return abort_printing_ ? CANCEL : FAILED; | 207 [print_info_ autorelease]; |
| 208 print_info_ = nil; |
| 209 context_ = NULL; |
| 210 } |
| 211 } |
| 212 |
| 213 gfx::NativeDrawingContext PrintingContextMac::context() const { |
| 214 return context_; |
| 215 } | 215 } |
| 216 | 216 |
| 217 } // namespace printing | 217 } // namespace printing |
| OLD | NEW |