Chromium Code Reviews| 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 ResetPrintInfo([panel printInfo]); |
| 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 ResetPrintInfo([[NSPrintInfo sharedPrintInfo] copy]); |
|
stuartmorgan
2011/06/02 21:07:20
copy + retain = leak
kmadhusu
2011/06/02 23:07:00
Fixed.
| |
| 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 ResetPrintInfo([[NSPrintInfo sharedPrintInfo]copy]); |
|
stuartmorgan
2011/06/02 21:07:20
copy + retain = leak
Also, why was the space remo
kmadhusu
2011/06/02 23:07:00
Fixed.
| |
| 103 print_info_.reset([[NSPrintInfo sharedPrintInfo] copy]); | |
| 104 | 106 |
| 105 bool collate; | 107 bool collate; |
| 106 bool color; | 108 bool color; |
| 107 bool landscape; | 109 bool landscape; |
| 108 bool print_to_pdf; | 110 bool print_to_pdf; |
| 109 int copies; | 111 int copies; |
| 110 int duplex_mode; | 112 int duplex_mode; |
| 111 std::string device_name; | 113 std::string device_name; |
| 112 | 114 |
| 113 if (!job_settings.GetBoolean(kSettingLandscape, &landscape) || | 115 if (!job_settings.GetBoolean(kSettingLandscape, &landscape) || |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 243 PMPrintSettings pmPrintSettings = | 245 PMPrintSettings pmPrintSettings = |
| 244 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); | 246 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); |
| 245 CFStringRef output_color = color ? NULL : kGrayColor; | 247 CFStringRef output_color = color ? NULL : kGrayColor; |
| 246 | 248 |
| 247 return PMPrintSettingsSetValue(pmPrintSettings, | 249 return PMPrintSettingsSetValue(pmPrintSettings, |
| 248 kColorModel, | 250 kColorModel, |
| 249 output_color, | 251 output_color, |
| 250 false) == noErr; | 252 false) == noErr; |
| 251 } | 253 } |
| 252 | 254 |
| 253 void PrintingContextMac::ParsePrintInfo(NSPrintInfo* print_info) { | 255 void PrintingContextMac::ResetPrintInfo(NSPrintInfo* print_info) { |
| 254 ResetSettings(); | 256 ResetSettings(); |
|
stuartmorgan
2011/06/02 21:07:20
I just looked at ResetSettings(), and it doesn't d
kmadhusu
2011/06/02 23:07:00
Done.
| |
| 255 print_info_.reset([print_info retain]); | 257 print_info_.reset([print_info retain]); |
| 258 } | |
| 259 | |
| 260 PageRanges PrintingContextMac::GetPageRangesFromPrintInfo() { | |
| 256 PageRanges page_ranges; | 261 PageRanges page_ranges; |
| 257 NSDictionary* print_info_dict = [print_info_.get() dictionary]; | 262 NSDictionary* print_info_dict = [print_info_.get() dictionary]; |
| 258 if (![[print_info_dict objectForKey:NSPrintAllPages] boolValue]) { | 263 if (![[print_info_dict objectForKey:NSPrintAllPages] boolValue]) { |
| 259 PageRange range; | 264 PageRange range; |
| 260 range.from = [[print_info_dict objectForKey:NSPrintFirstPage] intValue] - 1; | 265 range.from = [[print_info_dict objectForKey:NSPrintFirstPage] intValue] - 1; |
| 261 range.to = [[print_info_dict objectForKey:NSPrintLastPage] intValue] - 1; | 266 range.to = [[print_info_dict objectForKey:NSPrintLastPage] intValue] - 1; |
| 262 page_ranges.push_back(range); | 267 page_ranges.push_back(range); |
| 263 } | 268 } |
| 264 InitPrintSettingsFromPrintInfo(page_ranges); | 269 return page_ranges; |
| 265 } | 270 } |
| 266 | 271 |
| 267 PrintingContext::Result PrintingContextMac::InitWithSettings( | 272 PrintingContext::Result PrintingContextMac::InitWithSettings( |
| 268 const PrintSettings& settings) { | 273 const PrintSettings& settings) { |
| 269 DCHECK(!in_print_job_); | 274 DCHECK(!in_print_job_); |
| 270 | 275 |
| 271 settings_ = settings; | 276 settings_ = settings; |
| 272 | 277 |
| 273 NOTIMPLEMENTED(); | 278 NOTIMPLEMENTED(); |
| 274 | 279 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 366 void PrintingContextMac::ReleaseContext() { | 371 void PrintingContextMac::ReleaseContext() { |
| 367 print_info_.reset(); | 372 print_info_.reset(); |
| 368 context_ = NULL; | 373 context_ = NULL; |
| 369 } | 374 } |
| 370 | 375 |
| 371 gfx::NativeDrawingContext PrintingContextMac::context() const { | 376 gfx::NativeDrawingContext PrintingContextMac::context() const { |
| 372 return context_; | 377 return context_; |
| 373 } | 378 } |
| 374 | 379 |
| 375 } // namespace printing | 380 } // namespace printing |
| OLD | NEW |