diff --git a/src/add-ons/print/drivers/preview/PageSetupWindow.cpp b/src/add-ons/print/drivers/preview/PageSetupWindow.cpp index 07b3b10e53..5395d6be72 100644 --- a/src/add-ons/print/drivers/preview/PageSetupWindow.cpp +++ b/src/add-ons/print/drivers/preview/PageSetupWindow.cpp @@ -116,10 +116,9 @@ PageSetupWindow::PageSetupWindow(BMessage *msg, const char *printerName) // load orientation fSetupMsg->FindInt32("orientation", &orient); -// (new BAlert("", "orientation not in msg", "Shit"))->Go(); // load page rect - if (fSetupMsg->FindRect("paper_rect", &r) == B_OK) { + if (fSetupMsg->FindRect("preview:paper_rect", &r) == B_OK) { width = r.Width(); height = r.Height(); page = r; @@ -142,7 +141,7 @@ PageSetupWindow::PageSetupWindow(BMessage *msg, const char *printerName) // re-calculate the margin from the printable rect in points margin = page; - if (fSetupMsg->FindRect("printable_rect", &margin) == B_OK) { + if (fSetupMsg->FindRect("preview:printable_rect", &margin) == B_OK) { margin.top -= page.top; margin.left -= page.left; margin.right = page.right - margin.right; @@ -238,7 +237,7 @@ PageSetupWindow::PageSetupWindow(BMessage *msg, const char *printerName) BString scale; float scale0; if (fSetupMsg->FindFloat("scale", &scale0) == B_OK) { - scale << (int)(100.0 * scale0); + scale << (int)scale0; } else { scale = "100"; } @@ -295,13 +294,18 @@ PageSetupWindow::UpdateSetupMessage() if (item) { BMessage *msg = item->Message(); msg->FindInt32("orientation", &orientation); - if (fSetupMsg->HasInt32("orientation", orientation)) { - fSetupMsg->ReplaceInt32("orientation", orientation); - } else { - fSetupMsg->AddInt32("orientation", orientation); - } + SetInt32(fSetupMsg, "orientation", orientation); } + // Save scaling factor + float scale = atoi(fScaleControl->Text()); + if (scale <= 0.0) { // sanity check + scale = 100.0; + } + SetFloat(fSetupMsg, "scale", scale); + + float scaleR = 100.0 / scale; + item = fPageSizeMenu->Menu()->FindMarked(); if (item) { float w, h; @@ -309,15 +313,13 @@ PageSetupWindow::UpdateSetupMessage() msg->FindFloat("width", &w); msg->FindFloat("height", &h); BRect r; - if (orientation == 0) + if (orientation == 0) { r.Set(0, 0, w, h); - else - r.Set(0, 0, h, w); - if (fSetupMsg->HasRect("paper_rect")) { - fSetupMsg->ReplaceRect("paper_rect", r); } else { - fSetupMsg->AddRect("paper_rect", r); + r.Set(0, 0, h, w); } + SetRect(fSetupMsg, "preview:paper_rect", r); + SetRect(fSetupMsg, "paper_rect", ScaleRect(r, scaleR)); // Save the printable_rect BRect margin = fMarginView->GetMargin(); @@ -328,31 +330,13 @@ PageSetupWindow::UpdateSetupMessage() margin.right = h - margin.right; margin.bottom = w - margin.bottom; } - if (fSetupMsg->HasRect("printable_rect")) { - fSetupMsg->ReplaceRect("printable_rect", margin); - } else { - fSetupMsg->AddRect("printable_rect", margin); - } + SetRect(fSetupMsg, "preview:printable_rect", margin); + SetRect(fSetupMsg, "printable_rect", ScaleRect(margin, scaleR)); // save the units used int32 units = fMarginView->GetUnits(); - if (fSetupMsg->HasInt32("units")) { - fSetupMsg->ReplaceInt32("units", units); - } else { - fSetupMsg->AddInt32("units", units); - } - } - - // scaling factor - float scale = atoi(fScaleControl->Text()) / 100.0; - if (scale <= 0.0) { // sanity check - scale = 1.0; - } - if (fSetupMsg->HasFloat("scale")) { - fSetupMsg->ReplaceFloat("scale", scale); - } else { - fSetupMsg->AddFloat("scale", scale); - } + SetInt32(fSetupMsg, "units", units); + } } diff --git a/src/add-ons/print/drivers/preview/Preview.cpp b/src/add-ons/print/drivers/preview/Preview.cpp index 6c4cf7c3a5..55bfe6e5fc 100644 --- a/src/add-ons/print/drivers/preview/Preview.cpp +++ b/src/add-ons/print/drivers/preview/Preview.cpp @@ -29,6 +29,7 @@ THE SOFTWARE. #include #include +#include "Utils.h" #include "Preview.h" @@ -92,16 +93,25 @@ PreviewView::~PreviewView() { float PreviewView::ZoomFactor() const { const int32 b = 4; int32 zoom; - if (fZoom > 0) zoom = (1 << b) << fZoom; - else zoom = (1 << b) >> -fZoom; - return zoom / (float)(1 << b); + if (fZoom > 0) { + zoom = (1 << b) << fZoom; + } else { + zoom = (1 << b) >> -fZoom; + } + float factor = zoom / (float)(1 << b); + return factor * fReader.GetScale() / 100.0; } BRect PreviewView::PageRect() const { float f = ZoomFactor(); BRect r = fReader.PaperRect(); - r.left *= f; r.right *= f; r.top *= f; r.bottom *= f; - return r; + return ScaleRect(r, f); +} + +BRect PreviewView::PrintableRect() const { + float f = ZoomFactor(); + BRect r = fReader.PrintableRect(); + return ScaleRect(r, f); } BRect PreviewView::ViewRect() const { @@ -165,9 +175,9 @@ void PreviewView::DrawPageFrame(BRect rect) { void PreviewView::DrawPage(BRect rect) -{ - // constrain clipping region to paper dimensions - BRect r(PageRect()); +{ + // constrain clipping region to printable rectangle + BRect r(PrintableRect()); r.OffsetBy(kPreviewLeftMargin, kPreviewTopMargin); BRegion clip(r); ConstrainClippingRegion(&clip); @@ -177,11 +187,13 @@ void PreviewView::DrawPage(BRect rect) // print job coordinates are relative to the printable rect BRect printRect = fReader.PrintableRect(); - SetOrigin(kPreviewLeftMargin + printRect.left*ZoomFactor(), - kPreviewTopMargin + printRect.top*ZoomFactor() ); + SetOrigin(kPreviewLeftMargin + printRect.left * ZoomFactor(), + kPreviewTopMargin + printRect.top * ZoomFactor()); + + SetScale(ZoomFactor()); - SetScale(ZoomFactor() * fReader.GetScale()); fCachedPage->Draw(this); + PopState(); } diff --git a/src/add-ons/print/drivers/preview/Preview.h b/src/add-ons/print/drivers/preview/Preview.h index 1ac93173bb..a6430cfa09 100644 --- a/src/add-ons/print/drivers/preview/Preview.h +++ b/src/add-ons/print/drivers/preview/Preview.h @@ -56,6 +56,7 @@ class PreviewView : public BView { float ZoomFactor() const; BRect PageRect() const; + BRect PrintableRect() const; public: PreviewView(BFile* jobFile, BRect rect); diff --git a/src/add-ons/print/drivers/preview/Utils.cpp b/src/add-ons/print/drivers/preview/Utils.cpp index c0bfe4f432..ddf4884ddb 100644 --- a/src/add-ons/print/drivers/preview/Utils.cpp +++ b/src/add-ons/print/drivers/preview/Utils.cpp @@ -104,4 +104,34 @@ void AddString(BMessage* m, const char* name, const char* value) { } } +void SetRect(BMessage* msg, const char* name, BRect rect) { + if (msg->HasRect(name)) { + msg->ReplaceRect(name, rect); + } else { + msg->AddRect(name, rect); + } +} +void SetFloat(BMessage* msg, const char* name, float value) { + if (msg->HasFloat(name)) { + msg->ReplaceFloat(name, value); + } else { + msg->AddFloat(name, value); + } +} + +void SetInt32(BMessage* msg, const char* name, int32 value) { + if (msg->HasInt32(name)) { + msg->ReplaceInt32(name, value); + } else { + msg->AddInt32(name, value); + } +} + +BRect ScaleRect(BRect rect, float scale) { + rect.left *= scale; + rect.right *= scale; + rect.top *= scale; + rect.bottom *= scale; + return rect; +} \ No newline at end of file diff --git a/src/add-ons/print/drivers/preview/Utils.h b/src/add-ons/print/drivers/preview/Utils.h index 663728d13a..2c7402a7d7 100644 --- a/src/add-ons/print/drivers/preview/Utils.h +++ b/src/add-ons/print/drivers/preview/Utils.h @@ -41,6 +41,14 @@ THE SOFTWARE. void AddFields(BMessage* to, const BMessage* from, const char* excludeList[] = NULL, const char* includeList[] = NULL); void AddString(BMessage* m, const char* name, const char* value); +// set or replace a value in a BMessage +void SetRect(BMessage* msg, const char* name, BRect rect); +void SetFloat(BMessage* msg, const char* name, float value); +void SetInt32(BMessage* msg, const char* name, int32 value); + +// scalar multiplication +BRect ScaleRect(BRect rect, float scale); + class EscapeMessageFilter : public BMessageFilter { private: @@ -151,5 +159,4 @@ public: inline float scale(float f) { return fScale * f; } }; - #endif