[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "printing/printing_context_linux.h" |
| 6 | |
| 7 | #include "base/logging.h" |
| 8 | #include "base/values.h" |
| 9 | #include "printing/metafile.h" |
| 10 | #include "printing/print_dialog_gtk_interface.h" |
| 11 | #include "printing/print_job_constants.h" |
| 12 | #include "printing/units.h" |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | // Function pointer for creating print dialogs. |callback| is only used when |
| 17 | // |show_dialog| is true. |
| 18 | printing::PrintDialogGtkInterface* (*create_dialog_func_)( |
| 19 | printing::PrintingContextLinux* context) = NULL; |
| 20 | |
| 21 | // Function pointer for determining paper size. |
| 22 | gfx::Size (*get_pdf_paper_size_)( |
| 23 | printing::PrintingContextLinux* context) = NULL; |
| 24 | |
| 25 | } // namespace |
| 26 | |
| 27 | namespace printing { |
| 28 | |
| 29 | // static |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 30 | scoped_ptr<PrintingContext> PrintingContext::Create(Delegate* delegate) { |
| 31 | return make_scoped_ptr<PrintingContext>(new PrintingContextLinux(delegate)); |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 32 | } |
| 33 | |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 34 | PrintingContextLinux::PrintingContextLinux(Delegate* delegate) |
| 35 | : PrintingContext(delegate), print_dialog_(NULL) { |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | PrintingContextLinux::~PrintingContextLinux() { |
| 39 | ReleaseContext(); |
| 40 | |
| 41 | if (print_dialog_) |
| 42 | print_dialog_->ReleaseDialog(); |
| 43 | } |
| 44 | |
| 45 | // static |
| 46 | void PrintingContextLinux::SetCreatePrintDialogFunction( |
| 47 | PrintDialogGtkInterface* (*create_dialog_func)( |
| 48 | PrintingContextLinux* context)) { |
| 49 | DCHECK(create_dialog_func); |
| 50 | DCHECK(!create_dialog_func_); |
| 51 | create_dialog_func_ = create_dialog_func; |
| 52 | } |
| 53 | |
| 54 | // static |
| 55 | void PrintingContextLinux::SetPdfPaperSizeFunction( |
| 56 | gfx::Size (*get_pdf_paper_size)(PrintingContextLinux* context)) { |
| 57 | DCHECK(get_pdf_paper_size); |
| 58 | DCHECK(!get_pdf_paper_size_); |
| 59 | get_pdf_paper_size_ = get_pdf_paper_size; |
| 60 | } |
| 61 | |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 62 | void PrintingContextLinux::PrintDocument(const MetafilePlayer& metafile) { |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 63 | DCHECK(print_dialog_); |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 64 | print_dialog_->PrintDocument(metafile, document_name_); |
| 65 | } |
| 66 | |
| 67 | void PrintingContextLinux::AskUserForSettings( |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 68 | int max_pages, |
| 69 | bool has_selection, |
dgn | 4c172eea | 2014-12-15 21:11:23 | [diff] [blame] | 70 | bool is_scripted, |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 71 | const PrintSettingsCallback& callback) { |
[email protected] | bdf12cb7 | 2014-02-07 22:48:08 | [diff] [blame] | 72 | if (!print_dialog_) { |
| 73 | // Can only get here if the renderer is sending bad messages. |
| 74 | // http://crbug.com/341777 |
| 75 | NOTREACHED(); |
| 76 | callback.Run(FAILED); |
| 77 | return; |
| 78 | } |
| 79 | |
Vitaly Buka | bd7c981 | 2014-08-26 08:57:54 | [diff] [blame] | 80 | print_dialog_->ShowDialog( |
| 81 | delegate_->GetParentView(), has_selection, callback); |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | PrintingContext::Result PrintingContextLinux::UseDefaultSettings() { |
| 85 | DCHECK(!in_print_job_); |
| 86 | |
| 87 | ResetSettings(); |
| 88 | |
| 89 | if (!create_dialog_func_) |
| 90 | return OK; |
| 91 | |
| 92 | if (!print_dialog_) { |
| 93 | print_dialog_ = create_dialog_func_(this); |
| 94 | print_dialog_->AddRefToDialog(); |
| 95 | } |
| 96 | print_dialog_->UseDefaultSettings(); |
| 97 | |
| 98 | return OK; |
| 99 | } |
| 100 | |
| 101 | gfx::Size PrintingContextLinux::GetPdfPaperSizeDeviceUnits() { |
| 102 | if (get_pdf_paper_size_) |
| 103 | return get_pdf_paper_size_(this); |
| 104 | |
| 105 | return gfx::Size(); |
| 106 | } |
| 107 | |
| 108 | PrintingContext::Result PrintingContextLinux::UpdatePrinterSettings( |
vitalybuka | 92ab8ce | 2014-08-26 23:41:45 | [diff] [blame] | 109 | bool external_preview, |
| 110 | bool show_system_dialog) { |
| 111 | DCHECK(!show_system_dialog); |
[email protected] | d53002f4 | 2014-01-14 16:08:56 | [diff] [blame] | 112 | DCHECK(!in_print_job_); |
| 113 | DCHECK(!external_preview) << "Not implemented"; |
| 114 | |
| 115 | if (!create_dialog_func_) |
| 116 | return OK; |
| 117 | |
| 118 | if (!print_dialog_) { |
| 119 | print_dialog_ = create_dialog_func_(this); |
| 120 | print_dialog_->AddRefToDialog(); |
| 121 | } |
| 122 | |
| 123 | if (!print_dialog_->UpdateSettings(&settings_)) |
| 124 | return OnError(); |
| 125 | |
| 126 | return OK; |
| 127 | } |
| 128 | |
| 129 | PrintingContext::Result PrintingContextLinux::InitWithSettings( |
| 130 | const PrintSettings& settings) { |
| 131 | DCHECK(!in_print_job_); |
| 132 | |
| 133 | settings_ = settings; |
| 134 | |
| 135 | return OK; |
| 136 | } |
| 137 | |
| 138 | PrintingContext::Result PrintingContextLinux::NewDocument( |
| 139 | const base::string16& document_name) { |
| 140 | DCHECK(!in_print_job_); |
| 141 | in_print_job_ = true; |
| 142 | |
| 143 | document_name_ = document_name; |
| 144 | |
| 145 | return OK; |
| 146 | } |
| 147 | |
| 148 | PrintingContext::Result PrintingContextLinux::NewPage() { |
| 149 | if (abort_printing_) |
| 150 | return CANCEL; |
| 151 | DCHECK(in_print_job_); |
| 152 | |
| 153 | // Intentional No-op. |
| 154 | |
| 155 | return OK; |
| 156 | } |
| 157 | |
| 158 | PrintingContext::Result PrintingContextLinux::PageDone() { |
| 159 | if (abort_printing_) |
| 160 | return CANCEL; |
| 161 | DCHECK(in_print_job_); |
| 162 | |
| 163 | // Intentional No-op. |
| 164 | |
| 165 | return OK; |
| 166 | } |
| 167 | |
| 168 | PrintingContext::Result PrintingContextLinux::DocumentDone() { |
| 169 | if (abort_printing_) |
| 170 | return CANCEL; |
| 171 | DCHECK(in_print_job_); |
| 172 | |
| 173 | ResetSettings(); |
| 174 | return OK; |
| 175 | } |
| 176 | |
| 177 | void PrintingContextLinux::Cancel() { |
| 178 | abort_printing_ = true; |
| 179 | in_print_job_ = false; |
| 180 | } |
| 181 | |
| 182 | void PrintingContextLinux::ReleaseContext() { |
| 183 | // Intentional No-op. |
| 184 | } |
| 185 | |
| 186 | gfx::NativeDrawingContext PrintingContextLinux::context() const { |
| 187 | // Intentional No-op. |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | } // namespace printing |