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

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::RotatePageMarginsAndUpdateCurrentValue(
23 const PageMargins& margins) {
24 header = margins.header;
25 footer = margins.footer;
26 top = margins.right;
27 left = margins.top;
28 bottom = margins.left;
29 right = margins.bottom;
30 }
31
22 void PageMargins::Clear() { 32 void PageMargins::Clear() {
23 header = 0; 33 header = 0;
24 footer = 0; 34 footer = 0;
25 left = 0; 35 left = 0;
26 right = 0; 36 right = 0;
27 top = 0; 37 top = 0;
28 bottom = 0; 38 bottom = 0;
29 } 39 }
30 40
31 bool PageMargins::Equals(const PageMargins& rhs) const { 41 bool PageMargins::Equals(const PageMargins& rhs) const {
32 return header == rhs.header && 42 return header == rhs.header &&
33 footer == rhs.footer && 43 footer == rhs.footer &&
34 left == rhs.left && 44 left == rhs.left &&
35 top == rhs.top && 45 top == rhs.top &&
36 right == rhs.right && 46 right == rhs.right &&
37 bottom == rhs.bottom; 47 bottom == rhs.bottom;
38 } 48 }
39 49
40 PageSetup::PageSetup() { 50 PageSetup::PageSetup() : forced_margins_(false) {
41 Clear(); 51 Clear();
42 } 52 }
43 53
44 PageSetup::~PageSetup() {} 54 PageSetup::~PageSetup() {}
45 55
46 void PageSetup::Clear() { 56 void PageSetup::Clear() {
47 physical_size_.SetSize(0, 0); 57 physical_size_.SetSize(0, 0);
48 printable_area_.SetRect(0, 0, 0, 0); 58 printable_area_.SetRect(0, 0, 0, 0);
49 overlay_area_.SetRect(0, 0, 0, 0); 59 overlay_area_.SetRect(0, 0, 0, 0);
50 content_area_.SetRect(0, 0, 0, 0); 60 content_area_.SetRect(0, 0, 0, 0);
51 effective_margins_.Clear(); 61 effective_margins_.Clear();
52 text_height_ = 0; 62 text_height_ = 0;
vandebo (ex-Chrome) 2011/11/03 17:26:35 clear should probably reset forced_margins_
kmadhusu 2011/11/03 20:46:54 oops. Fixed.
53 } 63 }
54 64
55 bool PageSetup::Equals(const PageSetup& rhs) const { 65 bool PageSetup::Equals(const PageSetup& rhs) const {
56 return physical_size_ == rhs.physical_size_ && 66 return physical_size_ == rhs.physical_size_ &&
57 printable_area_ == rhs.printable_area_ && 67 printable_area_ == rhs.printable_area_ &&
58 overlay_area_ == rhs.overlay_area_ && 68 overlay_area_ == rhs.overlay_area_ &&
59 content_area_ == rhs.content_area_ && 69 content_area_ == rhs.content_area_ &&
60 effective_margins_.Equals(rhs.effective_margins_) && 70 effective_margins_.Equals(rhs.effective_margins_) &&
61 requested_margins_.Equals(rhs.requested_margins_) && 71 requested_margins_.Equals(rhs.requested_margins_) &&
62 text_height_ == rhs.text_height_; 72 text_height_ == rhs.text_height_;
63 } 73 }
64 74
65 void PageSetup::Init(const gfx::Size& physical_size, 75 void PageSetup::Init(const gfx::Size& physical_size,
66 const gfx::Rect& printable_area, 76 const gfx::Rect& printable_area,
67 int text_height) { 77 int text_height) {
68 DCHECK_LE(printable_area.right(), physical_size.width()); 78 DCHECK_LE(printable_area.right(), physical_size.width());
69 // I've seen this assert triggers on Canon GP160PF PCL 5e and HP LaserJet 5. 79 // 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. 80 // Since we don't know the dpi here, just disable the check.
71 // DCHECK_LE(printable_area.bottom(), physical_size.height()); 81 // DCHECK_LE(printable_area.bottom(), physical_size.height());
72 DCHECK_GE(printable_area.x(), 0); 82 DCHECK_GE(printable_area.x(), 0);
73 DCHECK_GE(printable_area.y(), 0); 83 DCHECK_GE(printable_area.y(), 0);
74 DCHECK_GE(text_height, 0); 84 DCHECK_GE(text_height, 0);
75 physical_size_ = physical_size; 85 physical_size_ = physical_size;
76 printable_area_ = printable_area; 86 printable_area_ = printable_area;
77 text_height_ = text_height; 87 text_height_ = text_height;
78 88
79 CalculateSizesWithinRect(printable_area_, text_height_); 89 CalculateSizesWithinRect(printable_area_, text_height_);
vandebo (ex-Chrome) 2011/11/03 17:26:35 This should call SetRequestedMarginsANdUpdateValue
kmadhusu 2011/11/03 20:46:54 Done.
80 } 90 }
81 91
82 void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) { 92 void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) {
83 requested_margins_ = requested_margins; 93 forced_margins_ = false;
84 if (printable_area_.width() && printable_area_.height()) 94 SetRequestedMarginsAndUpdateValues(requested_margins);
85 CalculateSizesWithinRect(printable_area_, text_height_);
86 } 95 }
87 96
88 void PageSetup::ForceRequestedMargins(const PageMargins& requested_margins) { 97 void PageSetup::ForceRequestedMargins(const PageMargins& requested_margins) {
89 requested_margins_ = requested_margins; 98 forced_margins_ = true;
90 if (physical_size_.width() && physical_size_.height()) 99 SetRequestedMarginsAndUpdateValues(requested_margins);
91 CalculateSizesWithinRect(gfx::Rect(physical_size_), 0);
92 } 100 }
93 101
94 void PageSetup::FlipOrientation() { 102 void PageSetup::FlipOrientation() {
95 if (physical_size_.width() && physical_size_.height()) { 103 if (physical_size_.width() && physical_size_.height()) {
96 gfx::Size new_size(physical_size_.height(), physical_size_.width()); 104 gfx::Size new_size(physical_size_.height(), physical_size_.width());
97 int new_y = physical_size_.width() - 105 int new_y = physical_size_.width() -
98 (printable_area_.width() + printable_area_.x()); 106 (printable_area_.width() + printable_area_.x());
99 gfx::Rect new_printable_area(printable_area_.y(), 107 gfx::Rect new_printable_area(printable_area_.y(),
100 new_y, 108 new_y,
101 printable_area_.height(), 109 printable_area_.height(),
102 printable_area_.width()); 110 printable_area_.width());
103 Init(new_size, new_printable_area, text_height_); 111 Init(new_size, new_printable_area, text_height_);
112 PageMargins new_margins;
113 new_margins.RotatePageMarginsAndUpdateCurrentValue(requested_margins_);
114 SetRequestedMarginsAndUpdateValues(new_margins);
104 } 115 }
105 } 116 }
106 117
118 void PageSetup::SetRequestedMarginsAndUpdateValues(
119 const PageMargins& requested_margins) {
120 requested_margins_ = requested_margins;
121 if (forced_margins_) {
122 if (physical_size_.width() && physical_size_.height())
vandebo (ex-Chrome) 2011/11/03 17:26:35 Pull this 'if' up one scope so you don't have to r
kmadhusu 2011/11/03 20:46:54 if (physical_size_....) { if (forced_margins_)
123 CalculateSizesWithinRect(gfx::Rect(physical_size_), 0);
124 } else {
125 if (printable_area_.width() && printable_area_.height())
126 CalculateSizesWithinRect(printable_area_, text_height_);
127 }
128 }
129
107 void PageSetup::CalculateSizesWithinRect(const gfx::Rect& bounds, 130 void PageSetup::CalculateSizesWithinRect(const gfx::Rect& bounds,
108 int text_height) { 131 int text_height) {
109 // Calculate the effective margins. The tricky part. 132 // Calculate the effective margins. The tricky part.
110 effective_margins_.header = std::max(requested_margins_.header, 133 effective_margins_.header = std::max(requested_margins_.header,
111 bounds.y()); 134 bounds.y());
112 effective_margins_.footer = std::max(requested_margins_.footer, 135 effective_margins_.footer = std::max(requested_margins_.footer,
113 physical_size_.height() - 136 physical_size_.height() -
114 bounds.bottom()); 137 bounds.bottom());
115 effective_margins_.left = std::max(requested_margins_.left, 138 effective_margins_.left = std::max(requested_margins_.left,
116 bounds.x()); 139 bounds.x());
(...skipping 13 matching lines...) Expand all
130 overlay_area_.set_x(effective_margins_.left); 153 overlay_area_.set_x(effective_margins_.left);
131 overlay_area_.set_y(effective_margins_.header); 154 overlay_area_.set_y(effective_margins_.header);
132 overlay_area_.set_width(std::max(0, 155 overlay_area_.set_width(std::max(0,
133 physical_size_.width() - 156 physical_size_.width() -
134 effective_margins_.right - 157 effective_margins_.right -
135 overlay_area_.x())); 158 overlay_area_.x()));
136 overlay_area_.set_height(std::max(0, 159 overlay_area_.set_height(std::max(0,
137 physical_size_.height() - 160 physical_size_.height() -
138 effective_margins_.footer - 161 effective_margins_.footer -
139 overlay_area_.y())); 162 overlay_area_.y()));
140
141 // Calculate the content area. If the margins are excessive, the content_area 163 // Calculate the content area. If the margins are excessive, the content_area
142 // size will be (0, 0). 164 // size will be (0, 0).
143 content_area_.set_x(effective_margins_.left); 165 content_area_.set_x(effective_margins_.left);
144 content_area_.set_y(effective_margins_.top); 166 content_area_.set_y(effective_margins_.top);
145 content_area_.set_width(std::max(0, 167 content_area_.set_width(std::max(0,
146 physical_size_.width() - 168 physical_size_.width() -
147 effective_margins_.right - 169 effective_margins_.right -
148 content_area_.x())); 170 content_area_.x()));
149 content_area_.set_height(std::max(0, 171 content_area_.set_height(std::max(0,
150 physical_size_.height() - 172 physical_size_.height() -
151 effective_margins_.bottom - 173 effective_margins_.bottom -
152 content_area_.y())); 174 content_area_.y()));
153 } 175 }
154 176
155 } // namespace printing 177 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698