Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: printing/page_setup.cc

Issue 8351063: PrintPreview: [LINUX] Update the margin values after flipping the paper orientation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: '' Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/page_setup.h" 5 #include "printing/page_setup.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 10
11 namespace printing { 11 namespace printing {
12 12
13 PageMargins::PageMargins() 13 PageMargins::PageMargins()
14 : header(0), 14 : header(0),
15 footer(0), 15 footer(0),
16 left(0), 16 left(0),
17 right(0), 17 right(0),
18 top(0), 18 top(0),
19 bottom(0) { 19 bottom(0) {
20 } 20 }
21 21
22 void PageMargins::Rotate() {
23 int temp_right = right;
24 right = bottom;
25 bottom = left;
26 left = top;
27 top = temp_right;
28 }
29
22 void PageMargins::Clear() { 30 void PageMargins::Clear() {
23 header = 0; 31 header = 0;
24 footer = 0; 32 footer = 0;
25 left = 0; 33 left = 0;
26 right = 0; 34 right = 0;
27 top = 0; 35 top = 0;
28 bottom = 0; 36 bottom = 0;
29 } 37 }
30 38
31 bool PageMargins::Equals(const PageMargins& rhs) const { 39 bool PageMargins::Equals(const PageMargins& rhs) const {
32 return header == rhs.header && 40 return header == rhs.header &&
33 footer == rhs.footer && 41 footer == rhs.footer &&
34 left == rhs.left && 42 left == rhs.left &&
35 top == rhs.top && 43 top == rhs.top &&
36 right == rhs.right && 44 right == rhs.right &&
37 bottom == rhs.bottom; 45 bottom == rhs.bottom;
38 } 46 }
39 47
40 PageSetup::PageSetup() { 48 PageSetup::PageSetup() : forced_margins_(false) {
vandebo (ex-Chrome) 2011/11/03 21:12:57 nit: not needed because it's in Clear
kmadhusu 2011/11/03 22:37:39 Done.
41 Clear(); 49 Clear();
42 } 50 }
43 51
44 PageSetup::~PageSetup() {} 52 PageSetup::~PageSetup() {}
45 53
46 void PageSetup::Clear() { 54 void PageSetup::Clear() {
47 physical_size_.SetSize(0, 0); 55 physical_size_.SetSize(0, 0);
48 printable_area_.SetRect(0, 0, 0, 0); 56 printable_area_.SetRect(0, 0, 0, 0);
49 overlay_area_.SetRect(0, 0, 0, 0); 57 overlay_area_.SetRect(0, 0, 0, 0);
50 content_area_.SetRect(0, 0, 0, 0); 58 content_area_.SetRect(0, 0, 0, 0);
51 effective_margins_.Clear(); 59 effective_margins_.Clear();
52 text_height_ = 0; 60 text_height_ = 0;
61 forced_margins_ = false;
53 } 62 }
54 63
55 bool PageSetup::Equals(const PageSetup& rhs) const { 64 bool PageSetup::Equals(const PageSetup& rhs) const {
56 return physical_size_ == rhs.physical_size_ && 65 return physical_size_ == rhs.physical_size_ &&
57 printable_area_ == rhs.printable_area_ && 66 printable_area_ == rhs.printable_area_ &&
58 overlay_area_ == rhs.overlay_area_ && 67 overlay_area_ == rhs.overlay_area_ &&
59 content_area_ == rhs.content_area_ && 68 content_area_ == rhs.content_area_ &&
60 effective_margins_.Equals(rhs.effective_margins_) && 69 effective_margins_.Equals(rhs.effective_margins_) &&
61 requested_margins_.Equals(rhs.requested_margins_) && 70 requested_margins_.Equals(rhs.requested_margins_) &&
62 text_height_ == rhs.text_height_; 71 text_height_ == rhs.text_height_;
63 } 72 }
64 73
65 void PageSetup::Init(const gfx::Size& physical_size, 74 void PageSetup::Init(const gfx::Size& physical_size,
66 const gfx::Rect& printable_area, 75 const gfx::Rect& printable_area,
67 int text_height) { 76 int text_height) {
68 DCHECK_LE(printable_area.right(), physical_size.width()); 77 DCHECK_LE(printable_area.right(), physical_size.width());
69 // I've seen this assert triggers on Canon GP160PF PCL 5e and HP LaserJet 5. 78 // I've seen this assert triggers on Canon GP160PF PCL 5e and HP LaserJet 5.
70 // Since we don't know the dpi here, just disable the check. 79 // Since we don't know the dpi here, just disable the check.
71 // DCHECK_LE(printable_area.bottom(), physical_size.height()); 80 // DCHECK_LE(printable_area.bottom(), physical_size.height());
72 DCHECK_GE(printable_area.x(), 0); 81 DCHECK_GE(printable_area.x(), 0);
73 DCHECK_GE(printable_area.y(), 0); 82 DCHECK_GE(printable_area.y(), 0);
74 DCHECK_GE(text_height, 0); 83 DCHECK_GE(text_height, 0);
75 physical_size_ = physical_size; 84 physical_size_ = physical_size;
76 printable_area_ = printable_area; 85 printable_area_ = printable_area;
77 text_height_ = text_height; 86 text_height_ = text_height;
78 87
79 CalculateSizesWithinRect(printable_area_, text_height_); 88 SetRequestedMarginsAndUpdateValues(requested_margins_);
80 } 89 }
81 90
82 void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) { 91 void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) {
83 requested_margins_ = requested_margins; 92 forced_margins_ = false;
84 if (printable_area_.width() && printable_area_.height()) 93 SetRequestedMarginsAndUpdateValues(requested_margins);
85 CalculateSizesWithinRect(printable_area_, text_height_);
86 } 94 }
87 95
88 void PageSetup::ForceRequestedMargins(const PageMargins& requested_margins) { 96 void PageSetup::ForceRequestedMargins(const PageMargins& requested_margins) {
89 requested_margins_ = requested_margins; 97 forced_margins_ = true;
90 if (physical_size_.width() && physical_size_.height()) 98 SetRequestedMarginsAndUpdateValues(requested_margins);
91 CalculateSizesWithinRect(gfx::Rect(physical_size_), 0);
92 } 99 }
93 100
94 void PageSetup::FlipOrientation() { 101 void PageSetup::FlipOrientation() {
95 if (physical_size_.width() && physical_size_.height()) { 102 if (physical_size_.width() && physical_size_.height()) {
96 gfx::Size new_size(physical_size_.height(), physical_size_.width()); 103 gfx::Size new_size(physical_size_.height(), physical_size_.width());
97 int new_y = physical_size_.width() - 104 int new_y = physical_size_.width() -
98 (printable_area_.width() + printable_area_.x()); 105 (printable_area_.width() + printable_area_.x());
99 gfx::Rect new_printable_area(printable_area_.y(), 106 gfx::Rect new_printable_area(printable_area_.y(),
100 new_y, 107 new_y,
101 printable_area_.height(), 108 printable_area_.height(),
102 printable_area_.width()); 109 printable_area_.width());
110 requested_margins_.Rotate();
103 Init(new_size, new_printable_area, text_height_); 111 Init(new_size, new_printable_area, text_height_);
104 } 112 }
105 } 113 }
106 114
115 void PageSetup::SetRequestedMarginsAndUpdateValues(
116 const PageMargins& requested_margins) {
117 requested_margins_ = requested_margins;
118 if (physical_size_.width() && physical_size_.height()) {
119 if (forced_margins_)
120 CalculateSizesWithinRect(gfx::Rect(physical_size_), 0);
121 else
122 CalculateSizesWithinRect(printable_area_, text_height_);
123 }
124 }
125
107 void PageSetup::CalculateSizesWithinRect(const gfx::Rect& bounds, 126 void PageSetup::CalculateSizesWithinRect(const gfx::Rect& bounds,
108 int text_height) { 127 int text_height) {
109 // Calculate the effective margins. The tricky part. 128 // Calculate the effective margins. The tricky part.
110 effective_margins_.header = std::max(requested_margins_.header, 129 effective_margins_.header = std::max(requested_margins_.header,
111 bounds.y()); 130 bounds.y());
112 effective_margins_.footer = std::max(requested_margins_.footer, 131 effective_margins_.footer = std::max(requested_margins_.footer,
113 physical_size_.height() - 132 physical_size_.height() -
114 bounds.bottom()); 133 bounds.bottom());
115 effective_margins_.left = std::max(requested_margins_.left, 134 effective_margins_.left = std::max(requested_margins_.left,
116 bounds.x()); 135 bounds.x());
(...skipping 29 matching lines...) Expand all
146 physical_size_.width() - 165 physical_size_.width() -
147 effective_margins_.right - 166 effective_margins_.right -
148 content_area_.x())); 167 content_area_.x()));
149 content_area_.set_height(std::max(0, 168 content_area_.set_height(std::max(0,
150 physical_size_.height() - 169 physical_size_.height() -
151 effective_margins_.bottom - 170 effective_margins_.bottom -
152 content_area_.y())); 171 content_area_.y()));
153 } 172 }
154 173
155 } // namespace printing 174 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698