From 6e758d67b9900f5e7dd7f9f25093fa66afd208f2 Mon Sep 17 00:00:00 2001 From: Karsten Heimrich Date: Mon, 4 Aug 2008 23:29:40 +0000 Subject: [PATCH] * cleanup * sync against the preview driver * fixed totally broken multiple pages on single page preview git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26812 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/libs/print/libprint/Preview.h | 273 ++--- src/libs/print/libprint/Preview.cpp | 1319 ++++++++++++++++--------- 2 files changed, 999 insertions(+), 593 deletions(-) diff --git a/headers/libs/print/libprint/Preview.h b/headers/libs/print/libprint/Preview.h index 311eed9dc2..ce5d034442 100644 --- a/headers/libs/print/libprint/Preview.h +++ b/headers/libs/print/libprint/Preview.h @@ -1,139 +1,164 @@ /* - -Preview - -Copyright (c) 2002, 2003 OpenBeOS. -Copyright (c) 2005 Haiku. - -Author: - Michael Pfeiffer - Hartmut Reh - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -of the Software, and to permit persons to whom the Software is furnished to do -so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - -*/ - -#include -#include "PrintJobReader.h" + * Copyright 2002-2008, Haiku. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Michael Pfeiffer + * Hartmut Reh + * julun + */ #include "BlockingWindow.h" +#include "JobData.h" +#include "PrintJobReader.h" + + +#include + + +class BButton; +class BFile; +class BMessage; +class BPicture; +class BScrollView; +class BStringView; +class BTextControl; + + +// #pragma mark - PreviewPage + class PreviewPage { - int32 fPage; - int32 fNumberOfPictures; - BPicture* fPictures; - BPoint* fPoints; - BRect* fRects; - status_t fStatus; - public: - PreviewPage(int32 page, PrintJobPage* pjp); - ~PreviewPage(); - status_t InitCheck() const; - - int32 Page() const { return fPage; } - void Draw(BView* view); + PreviewPage(int32 page, PrintJobPage* pjp); + ~PreviewPage(); + + void Draw(BView* view, const BRect& printRect); + status_t InitCheck() const; + int32 Page() const { return fPage; } + +private: + int32 fPage; + status_t fStatus; + int32 fNumberOfPictures; + + BRect* fRects; + BPoint* fPoints; + BPicture* fPictures; }; + +// #pragma mark - PreviewView + + class PreviewView : public BView { - int32 fPage; - int32 fZoom; - PrintJobReader fReader; - int32 fNumberOfPagesPerPage; - int32 fNumberOfPages; // physical pages - bool fReverse; - BRect fPageRect; - JobData::Orientation fOrientation; - JobData::PageSelection fPageSelection; - PreviewPage* fCachedPage; - - float ZoomFactor() const; - BRect PageRect() const; - BRect PrintableRect() const; - int32 GetPageNumber(int32 index) const; - public: - PreviewView(BFile* jobFile, BRect rect); - ~PreviewView(); - status_t InitCheck() const; - - BRect ViewRect() const; + PreviewView(BFile* jobFile, BRect rect); + virtual ~PreviewView(); - bool IsPageLoaded(int32 page) const; - bool IsPageValid() const; - void LoadPage(int32 page); - void DrawPageFrame(BRect r); - void DrawPage(BRect r); - void Draw(BRect r); - void FrameResized(float width, float height); - - void FixScrollbars(); - - bool ShowsFirstPage() const; - bool ShowsLastPage() const; - int CurrentPage() const { return fPage + 1; } - int NumberOfPages() const; - void ShowNextPage(); - void ShowPrevPage(); - void ShowFirstPage(); - void ShowLastPage(); - void ShowFindPage(int page); - - bool CanZoomIn() const; - bool CanZoomOut() const; - void ZoomIn(); - void ZoomOut(); + virtual void Show(); + virtual void Hide(); + virtual void Draw(BRect r); + virtual void FrameResized(float width, float height); + virtual void MouseDown(BPoint point); + virtual void MouseMoved(BPoint point, uint32 transit, + const BMessage* message); + virtual void MouseUp(BPoint point); + virtual void KeyDown(const char* bytes, int32 numBytes); + + void ShowFirstPage(); + void ShowPrevPage(); + void ShowNextPage(); + void ShowLastPage(); + bool ShowsFirstPage() const; + bool ShowsLastPage() const; + void ShowFindPage(int32 page); + + void ZoomIn(); + bool CanZoomIn() const; + void ZoomOut(); + bool CanZoomOut() const; + + void FixScrollbars(); + BRect ViewRect() const; + status_t InitCheck() const; + int32 NumberOfPages() const; + int32 CurrentPage() const { return fPage + 1; } + +private: + BRect _PaperRect() const; + float _ZoomFactor() const; + BRect _PrintableRect() const; + + void _LoadPage(int32 page); + bool _IsPageValid() const; + bool _IsPageLoaded(int32 page) const; + + BRect _ContentRect() const; + void _DrawPageFrame(BRect rect); + void _DrawPage(BRect updateRect); + void _DrawMarginFrame(BRect rect); + + int32 _GetPageNumber(int32 index) const; + +private: + int32 fPage; + int32 fZoom; + PrintJobReader fReader; + bool fReverse; + BRect fPaperRect; + BRect fPrintableRect; + bool fTracking; + bool fInsideView; + BPoint fScrollStart; + int32 fNumberOfPages; + int32 fNumberOfPagesPerPage; + PreviewPage* fCachedPage; + + JobData::Orientation fOrientation; + JobData::PageSelection fPageSelection; }; + +// #pragma mark - PreviewWindow + + class PreviewWindow : public BlockingWindow { - BButton *fFirst; - BButton *fNext; - BButton *fPrev; - BButton *fLast; - BButton *fZoomIn; - BButton *fZoomOut; - BTextControl *fPageNumber; - BStringView *fPageText; - PreviewView *fPreview; - BScrollView *fPreviewScroller; - float fButtonBarHeight; - - enum { - MSG_FIRST_PAGE = 'pwfp', - MSG_NEXT_PAGE = 'pwnp', - MSG_PREV_PAGE = 'pwpp', - MSG_LAST_PAGE = 'pwlp', - MSG_FIND_PAGE = 'pwsp', - MSG_ZOOM_IN = 'pwzi', - MSG_ZOOM_OUT = 'pwzo', - MSG_PRINT_JOB = 'pwpj', - MSG_CANCEL_JOB = 'pwcj', - }; - - void ResizeToPage(bool allowShrinking = false); - void UpdateControls(); - - typedef BlockingWindow inherited; - public: - PreviewWindow(BFile* jobFile, bool showOkAndCancelButtons = false); - status_t InitCheck() const { return fPreview->InitCheck(); } - void MessageReceived(BMessage* m); - status_t Go(); -}; + PreviewWindow(BFile* jobFile, + bool showOkAndCancelButtons = false); + virtual void MessageReceived(BMessage* m); + + status_t Go(); + status_t InitCheck() const { return fPreview->InitCheck(); } + + +private: + void _ResizeToPage(); + void _UpdateControls(); + +private: + BButton* fFirst; + BButton* fNext; + BButton* fPrev; + BButton* fLast; + BButton* fZoomIn; + BButton* fZoomOut; + BTextControl* fPageNumber; + BStringView* fPageText; + PreviewView* fPreview; + BScrollView* fPreviewScroller; + float fButtonBarHeight; + + enum { + MSG_FIRST_PAGE = 'pwfp', + MSG_NEXT_PAGE = 'pwnp', + MSG_PREV_PAGE = 'pwpp', + MSG_LAST_PAGE = 'pwlp', + MSG_FIND_PAGE = 'pwsp', + MSG_ZOOM_IN = 'pwzi', + MSG_ZOOM_OUT = 'pwzo', + MSG_PRINT_JOB = 'pwpj', + MSG_CANCEL_JOB = 'pwcj', + }; +}; diff --git a/src/libs/print/libprint/Preview.cpp b/src/libs/print/libprint/Preview.cpp index f86290e344..46669c6013 100644 --- a/src/libs/print/libprint/Preview.cpp +++ b/src/libs/print/libprint/Preview.cpp @@ -1,121 +1,560 @@ /* - * Copyright 2002-2007, Haiku. All rights reserved. + * Copyright 2002-2008, Haiku. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: * Michael Pfeiffer * Hartmut Reh + * julun */ -#include - -#include -#include +#include "Preview.h" #include "GraphicsDriver.h" -#include "Preview.h" #include "PrintUtils.h" -// Implementation of PreviewPage +#include +#include + + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +// #pragma mark - PreviewPage + + PreviewPage::PreviewPage(int32 page, PrintJobPage* pjp) : fPage(page) - , fPictures(NULL) - , fPoints(NULL) + , fStatus(B_ERROR) + , fNumberOfPictures(0) , fRects(NULL) + , fPoints(NULL) + , fPictures(NULL) { fNumberOfPictures = pjp->NumberOfPictures(); - fPictures = new BPicture[fNumberOfPictures]; - fPoints = new BPoint[fNumberOfPictures]; + fRects = new BRect[fNumberOfPictures]; - status_t rc = B_ERROR; - for (int32 i = 0; i < fNumberOfPictures && - (rc = pjp->NextPicture(fPictures[i], fPoints[i], fRects[i])) == B_OK; i ++); - fStatus = rc; -} + fPoints = new BPoint[fNumberOfPictures]; + fPictures = new BPicture[fNumberOfPictures]; -PreviewPage::~PreviewPage() { - delete []fPictures; - delete []fPoints; - delete []fRects; -} - -status_t PreviewPage::InitCheck() const { - return fStatus; -} - -void PreviewPage::Draw(BView* view) -{ - ASSERT(fStatus == B_OK); - for (int32 i = 0; i < fNumberOfPictures; i ++) - { - view->DrawPicture(&fPictures[i], fPoints[i]); + for (int32 i = 0; i < fNumberOfPictures; ++i) { + fStatus = pjp->NextPicture(fPictures[i], fPoints[i], fRects[i]); + if (fStatus != B_OK) + break; } } -// Implementation of PreviewView -const float kPreviewTopMargin = 10; -const float kPreviewBottomMargin = 30; -const float kPreviewLeftMargin = 10; -const float kPreviewRightMargin = 30; +PreviewPage::~PreviewPage() + { + delete [] fRects; + delete [] fPoints; + delete [] fPictures; +} + + +status_t +PreviewPage::InitCheck() const +{ + return fStatus; +} + + +void +PreviewPage::Draw(BView* view, const BRect& printRect) +{ + ASSERT(fStatus == B_OK); + for (int32 i = 0; i < fNumberOfPictures; i++) + view->DrawPicture(&fPictures[i], printRect.LeftTop() + fPoints[i]); +} + + +// #pragma mark - PreviewView + + +namespace { + +const float kPreviewTopMargin = 10.0; +const float kPreviewBottomMargin = 30.0; +const float kPreviewLeftMargin = 10.0; +const float kPreviewRightMargin = 30.0; + // TODO share constant with JobData -static const char *kJDOrientation = "orientation"; -static const char *kJDNup = "JJJJ_nup"; -static const char *kJDReverse = "JJJJ_reverse"; -static const char* kJDPageSelection = "JJJJ_page_selection"; +const char *kJDOrientation = "orientation"; +const char *kJDNup = "JJJJ_nup"; +const char *kJDReverse = "JJJJ_reverse"; +const char* kJDPageSelection = "JJJJ_page_selection"; -static BRect RotateRect(BRect rect) + +const uint8 ZOOM_IN[] = { 16, 1, 6, 6, 0, 0, 15, 128, 48, 96, 32, 32, 66, 16, + 66, 16, 79, 144, 66, 16, 66, 16, 32, 32, 48, 112, 15, 184, 0, 28, 0, 14, 0, + 6, 0, 0, 15, 128, 63, 224, 127, 240, 127, 240, 255, 248, 255, 248, 255, 248, + 255, 248, 255, 248, 127, 248, 127, 248, 63, 252, 15, 254, 0, 31, 0, 15, 0, 7 }; + + +const uint8 ZOOM_OUT[] = { 16, 1, 6, 6, 0, 0, 15, 128, 48, 96, 32, 32, 64, 16, + 64, 16, 79, 144, 64, 16, 64, 16, 32, 32, 48, 112, 15, 184, 0, 28, 0, 14, 0, + 6, 0, 0, 15, 128, 63, 224, 127, 240, 127, 240, 255, 248, 255, 248, 255, 248, + 255, 248, 255, 248, 127, 248, 127, 248, 63, 252, 15, 254, 0, 31, 0, 15, 0, 7 }; + + +BRect +RotateRect(const BRect& rect) { - BRect rotated(rect.top, rect.left, rect.bottom, rect.right); - return rotated; + return BRect(rect.top, rect.left, rect.bottom, rect.right); } + +BRect +ScaleDown(BRect rect, float factor) +{ + rect.left /= factor; + rect.top /= factor; + rect.right /= factor; + rect.bottom /= factor; + return rect; +} + + +BPoint +CalulateOffset(int32 numberOfPagesPerPage, int32 index, + JobData::Orientation orientation, BRect printableRect) +{ + BPoint offset(0.0, 0.0); + if (numberOfPagesPerPage == 1) + return offset; + + float width = printableRect.Width(); + float height = printableRect.Height(); + + switch (numberOfPagesPerPage) { + case 2: { + if (index == 1) { + if (JobData::kPortrait == orientation) + offset.x = width; + else + offset.y = height; + } + } break; + + case 8: { + if (JobData::kPortrait == orientation) { + offset.x = width * (index / 2); + offset.y = height * (index % 2); + } else { + offset.x = width * (index % 2); + offset.y = height * (index / 2); + } + } break; + + case 32: { + if (JobData::kPortrait == orientation) { + offset.x = width * (index / 4); + offset.y = height * (index % 4); + } else { + offset.x = width * (index % 4); + offset.y = height * (index / 4); + } + } break; + + case 4: { + case 9: + case 16: + case 25: + case 36: + case 49: + case 64: + case 81: + case 100: + case 121: + int32 value = int32(sqrt(double(numberOfPagesPerPage))); + offset.x = width * (index % value); + offset.y = height * (index / value); + } break; + } + return offset; +} + +} + + PreviewView::PreviewView(BFile* jobFile, BRect rect) : BView(rect, "PreviewView", B_FOLLOW_ALL, B_WILL_DRAW | B_FRAME_EVENTS) , fPage(0) , fZoom(0) , fReader(jobFile) - , fNumberOfPagesPerPage(1) , fReverse(false) + , fPaperRect(BRect()) + , fPrintableRect(BRect()) + , fTracking(false) + , fInsideView(true) + , fScrollStart(0.0, 0.0) + , fNumberOfPages(1) + , fNumberOfPagesPerPage(1) + , fCachedPage(NULL) , fOrientation(JobData::kPortrait) , fPageSelection(JobData::kAllPages) - , fCachedPage(NULL) { int32 value32; - if (fReader.JobSettings()->FindInt32(kJDOrientation, &value32) == B_OK) { + if (fReader.JobSettings()->FindInt32(kJDOrientation, &value32) == B_OK) fOrientation = (JobData::Orientation)value32; - } - if (fReader.JobSettings()->FindInt32(kJDPageSelection, &value32) == B_OK) { + + if (fReader.JobSettings()->FindInt32(kJDPageSelection, &value32) == B_OK) fPageSelection = (JobData::PageSelection)value32; - } + fReader.JobSettings()->FindInt32(kJDNup, &fNumberOfPagesPerPage); fReader.JobSettings()->FindBool(kJDReverse, &fReverse); - fNumberOfPages = (fReader.NumberOfPages() + fNumberOfPagesPerPage - 1) / fNumberOfPagesPerPage; - if (fPageSelection == JobData::kOddNumberedPages) { + fNumberOfPages = (fReader.NumberOfPages() + fNumberOfPagesPerPage - 1) + / fNumberOfPagesPerPage; + + if (fPageSelection == JobData::kOddNumberedPages) fNumberOfPages = (fNumberOfPages + 1) / 2; - } - else if (fPageSelection == JobData::kEvenNumberedPages) { + else if (fPageSelection == JobData::kEvenNumberedPages) fNumberOfPages /= 2; - } - fPageRect = fReader.PaperRect(); + + fPaperRect = fReader.PaperRect(); + fPrintableRect = fReader.PrintableRect(); switch (fNumberOfPagesPerPage) { case 2: case 8: case 32: - case 128: - fPageRect = RotateRect(fPageRect); - break; + case 128: { + fPaperRect = RotateRect(fPaperRect); + fPrintableRect = RotateRect(fPrintableRect); + } break; } } -PreviewView::~PreviewView() { + +PreviewView::~PreviewView() +{ delete fCachedPage; } -// returns 2 ^ fZoom -float PreviewView::ZoomFactor() const { + +void +PreviewView::Show() +{ + BView::Show(); + be_app->SetCursor(ZOOM_IN); +} + + +void +PreviewView::Hide() +{ + be_app->SetCursor(B_HAND_CURSOR); + BView::Hide(); +} + + +void +PreviewView::Draw(BRect rect) +{ + if (fReader.InitCheck() == B_OK) { + _DrawPageFrame(rect); + _DrawPage(rect); + _DrawMarginFrame(rect); + } +} + + +void +PreviewView::FrameResized(float width, float height) +{ + Invalidate(); + FixScrollbars(); +} + + +void +PreviewView::MouseDown(BPoint point) +{ + MakeFocus(true); + BMessage *message = Window()->CurrentMessage(); + + int32 button; + if (message && message->FindInt32("buttons", &button) == B_OK) { + if (button == B_PRIMARY_MOUSE_BUTTON) { + int32 modifier; + if (message->FindInt32("modifiers", &modifier) == B_OK) { + if (modifier & B_SHIFT_KEY) + ZoomOut(); + else + ZoomIn(); + } + } + + if (button == B_SECONDARY_MOUSE_BUTTON) { + fTracking = true; + be_app->SetCursor(B_HAND_CURSOR); + SetMouseEventMask(B_POINTER_EVENTS, + B_LOCK_WINDOW_FOCUS | B_NO_POINTER_HISTORY); + fScrollStart = Bounds().LeftTop() + ConvertToScreen(point); + } + } +} + + +void +PreviewView::MouseMoved(BPoint point, uint32 transit, const BMessage* message) +{ + if (fTracking) { + uint32 button; + GetMouse(&point, &button, false); + point = fScrollStart - ConvertToScreen(point); + + float hMin, hMax; + BScrollBar *hBar = ScrollBar(B_HORIZONTAL); + hBar->GetRange(&hMin, &hMax); + + float vMin, vMax; + BScrollBar *vBar = ScrollBar(B_VERTICAL); + vBar->GetRange(&vMin, &vMax); + + if (point.x < 0.0) point.x = 0.0; + if (point.y < 0.0) point.y = 0.0; + if (point.x > hMax) point.x = hMax; + if (point.y > vMax) point.y = vMax; + + ScrollTo(point); + } else { + switch (transit) { + case B_ENTERED_VIEW: { + fInsideView = true; + be_app->SetCursor(ZOOM_IN); + } break; + + case B_EXITED_VIEW: { + fInsideView = false; + be_app->SetCursor(B_HAND_CURSOR); + } break; + + default: { + if (modifiers() & B_SHIFT_KEY) + be_app->SetCursor(ZOOM_OUT); + else + be_app->SetCursor(ZOOM_IN); + } break; + } + } +} + + +void +PreviewView::MouseUp(BPoint point) +{ + (void)point; + fTracking = false; + fScrollStart.Set(0.0, 0.0); + if (fInsideView && ((modifiers() & B_SHIFT_KEY) == 0)) + be_app->SetCursor(ZOOM_IN); +} + + +void +PreviewView::KeyDown(const char* bytes, int32 numBytes) +{ + MakeFocus(true); + switch (bytes[0]) { + case '-': { + if (modifiers() & B_CONTROL_KEY) + ZoomOut(); + } break; + + case '+' : { + if (modifiers() & B_CONTROL_KEY) + ZoomIn(); + } break; + + default: { + BView::KeyDown(bytes, numBytes); + } break; + } +} + + +void +PreviewView::ShowFirstPage() +{ + if (!ShowsFirstPage()) { + fPage = 0; + Invalidate(); + } +} + + +void +PreviewView::ShowPrevPage() +{ + if (!ShowsFirstPage()) { + fPage--; + Invalidate(); + } +} + + +void +PreviewView::ShowNextPage() +{ + if (!ShowsLastPage()) { + fPage++; + Invalidate(); + } +} + + +void +PreviewView::ShowLastPage() +{ + if (!ShowsLastPage()) { + fPage = NumberOfPages()-1; + Invalidate(); + } +} + + +bool +PreviewView::ShowsFirstPage() const +{ + return fPage == 0; +} + + +bool +PreviewView::ShowsLastPage() const +{ + return fPage == NumberOfPages() - 1; +} + + +void +PreviewView::ShowFindPage(int32 page) +{ + page--; + + if (page < 0) { + page = 0; + } else if (page > (NumberOfPages()-1)) { + page = NumberOfPages()-1; + } + + fPage = page; + Invalidate(); +} + + +void +PreviewView::ZoomIn() +{ + if (CanZoomIn()) { + fZoom++; + FixScrollbars(); + Invalidate(); + } +} + + +bool +PreviewView::CanZoomIn() const +{ + return fZoom < 4; +} + + +void +PreviewView::ZoomOut() +{ + if (CanZoomOut()) { + fZoom--; + FixScrollbars(); + Invalidate(); + } +} + + +bool +PreviewView::CanZoomOut() const +{ + return fZoom > -2; +} + + +void +PreviewView::FixScrollbars() +{ + float width = _PaperRect().Width() + kPreviewLeftMargin + kPreviewRightMargin; + float height = _PaperRect().Height() + kPreviewTopMargin + kPreviewBottomMargin; + + BRect frame(Bounds()); + float x = width - frame.Width(); + if (x < 0.0) + x = 0.0; + + float y = height - frame.Height(); + if (y < 0.0) + y = 0.0; + + BScrollBar * scroll = ScrollBar(B_HORIZONTAL); + scroll->SetRange(0.0, x); + scroll->SetProportion((width - x) / width); + float bigStep = frame.Width() - 2; + float smallStep = bigStep / 10.; + scroll->SetSteps(smallStep, bigStep); + + scroll = ScrollBar(B_VERTICAL); + scroll->SetRange(0.0, y); + scroll->SetProportion((height - y) / height); + bigStep = frame.Height() - 2; + smallStep = bigStep / 10.; + scroll->SetSteps(smallStep, bigStep); +} + + +BRect +PreviewView::ViewRect() const +{ + BRect r(_PaperRect()); + r.right += kPreviewLeftMargin + kPreviewRightMargin; + r.bottom += kPreviewTopMargin + kPreviewBottomMargin; + return r; +} + + +status_t +PreviewView::InitCheck() const +{ + return fReader.InitCheck(); +} + + +int32 +PreviewView::NumberOfPages() const +{ + return fNumberOfPages; +} + + +BRect +PreviewView::_PaperRect() const +{ + return ScaleRect(fPaperRect, _ZoomFactor()); +} + + +float +PreviewView::_ZoomFactor() const +{ const int32 b = 4; int32 zoom; if (fZoom > 0) { @@ -127,493 +566,435 @@ float PreviewView::ZoomFactor() const { return factor * fReader.GetScale() / 100.0; } -BRect PreviewView::PageRect() const { - float f = ZoomFactor(); - BRect r(fPageRect); - return ScaleRect(r, f); + +BRect +PreviewView::_PrintableRect() const +{ + return ScaleRect(fPrintableRect, _ZoomFactor()); } -BRect PreviewView::PrintableRect() const { - float f = ZoomFactor(); - BRect r = fReader.PrintableRect(); - return ScaleRect(r, f); -} -BRect PreviewView::ViewRect() const { - BRect r(PageRect()); - r.right += kPreviewLeftMargin + kPreviewRightMargin; - r.bottom += kPreviewTopMargin + kPreviewBottomMargin; - return r; -} - -status_t PreviewView::InitCheck() const { - return fReader.InitCheck(); -} - -bool PreviewView::IsPageLoaded(int32 page) const { - return fCachedPage != NULL && fCachedPage->Page() == page; -} - -bool PreviewView::IsPageValid() const { +bool +PreviewView::_IsPageValid() const +{ return fCachedPage && fCachedPage->InitCheck() == B_OK; } -void PreviewView::LoadPage(int32 page) { - delete fCachedPage; + +void +PreviewView::_LoadPage(int32 page) +{ + delete fCachedPage; fCachedPage = NULL; + PrintJobPage pjp; - if (fReader.GetPage(page, pjp) == B_OK) { + if (fReader.GetPage(page, pjp) == B_OK) fCachedPage = new PreviewPage(page, &pjp); - } } -void PreviewView::DrawPageFrame(BRect rect) { + +bool +PreviewView::_IsPageLoaded(int32 page) const +{ + return fCachedPage != NULL && fCachedPage->Page() == page; +} + + +BRect +PreviewView::_ContentRect() const +{ + float offsetX = kPreviewLeftMargin; + float offsetY = kPreviewTopMargin; + + BRect rect = Bounds(); + BRect paperRect = _PaperRect(); + + float min, max; + ScrollBar(B_HORIZONTAL)->GetRange(&min, &max); + if (min == max) { + if ((paperRect.right + 2 * offsetX) < rect.right) + offsetX = (rect.right - (paperRect.right + 2 * offsetX)) / 2; + } + + ScrollBar(B_VERTICAL)->GetRange(&min, &max); + if (min == max) { + if ((paperRect.bottom + 2 * offsetY) < rect.bottom) + offsetY = (rect.bottom - (paperRect.bottom + 2 * offsetY)) / 2; + } + + paperRect.OffsetTo(offsetX, offsetY); + return paperRect; +} + + +void +PreviewView::_DrawPageFrame(BRect rect) +{ const float kShadowIndent = 3; const float kShadowWidth = 3; - float x, y; - float right, bottom; - rgb_color frameColor = {0, 0, 0, 0}; - rgb_color shadowColor = {90, 90, 90, 0}; - BRect r(PageRect()); - + + const rgb_color frameColor = { 0, 0, 0, 0 }; + const rgb_color shadowColor = { 90, 90, 90, 0 }; + PushState(); + // draw page border around page - r.InsetBy(-1, -1); - r.OffsetTo(kPreviewLeftMargin-1, kPreviewTopMargin-1); + BRect r(_ContentRect().InsetByCopy(-1, -1)); + SetHighColor(frameColor); StrokeRect(r); - + // draw page shadow SetHighColor(shadowColor); - x = r.right + 1; - right = x + kShadowWidth; - bottom = r.bottom + 1 + kShadowWidth; - y = r.top + kShadowIndent; + float x = r.right + 1; + float right = x + kShadowWidth; + float bottom = r.bottom + 1 + kShadowWidth; + float y = r.top + kShadowIndent; FillRect(BRect(x, y, right, bottom)); x = r.left + kShadowIndent; y = r.bottom + 1; FillRect(BRect(x, y, r.right, bottom)); + PopState(); } -int32 PreviewView::GetPageNumber(int32 index) const -{ - int page = fPage; - if (fReverse) { - page = fNumberOfPages - fPage - 1; - } - if (fPageSelection == JobData::kOddNumberedPages) { +void PreviewView::_DrawPage(BRect rect) +{ + BRect printRect(_PrintableRect()); + switch (fNumberOfPagesPerPage) { + case 2: + case 8: + case 32: + case 128: { + printRect = RotateRect(printRect); + } break; + } + printRect.OffsetBy(_ContentRect().LeftTop()); + + BPoint scalingXY = GraphicsDriver::getScale(fNumberOfPagesPerPage, printRect, 100.0); + float scaling = min_c(scalingXY.x, scalingXY.y); + + printRect = ScaleDown(printRect, _ZoomFactor() * scaling); + BRect clipRect(ScaleRect(printRect, scaling)); + + for (int32 index = 0; index < fNumberOfPagesPerPage; ++index) { + int32 pageNumber = _GetPageNumber(index); + if (pageNumber < 0) + continue; + + if (!_IsPageLoaded(pageNumber)) + _LoadPage(pageNumber); + + if (!_IsPageValid()) + continue; + + BPoint offset(CalulateOffset(fNumberOfPagesPerPage, index, fOrientation, + clipRect)); + + clipRect.OffsetTo(printRect.LeftTop()); + clipRect.OffsetBy(offset); + + BRegion clip(clipRect); + ConstrainClippingRegion(&clip); + + SetScale(_ZoomFactor() * scaling); + + fCachedPage->Draw(this, printRect.OffsetByCopy(offset)); + + if (fNumberOfPagesPerPage > 1) + StrokeRect(clipRect.InsetByCopy(1.0, 1.0), B_MIXED_COLORS); + + SetScale(1.0); + + ConstrainClippingRegion(NULL); + } +} + + +void +PreviewView::_DrawMarginFrame(BRect rect) +{ + BRect paperRect(_ContentRect()); + BRect printRect(_PrintableRect()); + printRect.OffsetBy(paperRect.LeftTop()); + + const rgb_color highColor = HighColor(); + const rgb_color white = { 255, 255, 255, 255 }; + + SetHighColor(white); + + FillRect(BRect(paperRect.left, paperRect.top, printRect.left + , paperRect.bottom)); + FillRect(BRect(paperRect.left, paperRect.top, paperRect.right + , printRect.top)); + FillRect(BRect(printRect.right, paperRect.top, paperRect.right + , paperRect.bottom)); + FillRect(BRect(paperRect.left, printRect.bottom, paperRect.right + , paperRect.bottom)); + + SetHighColor(highColor); + + BeginLineArray(4); + + SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR)); + StrokeLine(BPoint(printRect.left, paperRect.top), + BPoint(printRect.left, paperRect.bottom), B_MIXED_COLORS); + StrokeLine(BPoint(printRect.right, paperRect.top), + BPoint(printRect.right, paperRect.bottom), B_MIXED_COLORS); + StrokeLine(BPoint(paperRect.left, printRect.top), + BPoint(paperRect.right, printRect.top), B_MIXED_COLORS); + StrokeLine(BPoint(paperRect.left, printRect.bottom), + BPoint(paperRect.right, printRect.bottom), B_MIXED_COLORS); + + EndLineArray(); +} + + + +int32 PreviewView::_GetPageNumber(int32 index) const +{ + int32 page = fPage; + if (fReverse) + page = fNumberOfPages - fPage - 1; + + if (fPageSelection == JobData::kOddNumberedPages) page *= 2; // 0, 2, 4, ... - } - else if (fPageSelection == JobData::kEvenNumberedPages) { + else if (fPageSelection == JobData::kEvenNumberedPages) page = 2 * page + 1; // 1, 3, 5, ... - } return page * fNumberOfPagesPerPage + index; } -void PreviewView::DrawPage(BRect rect) -{ - BRect physicalRect = fReader.PaperRect(); - BRect scaledPhysicalRect = physicalRect; - BRect scaledPrintableRect = fReader.PrintableRect(); - - for (int32 index = 0; index < fNumberOfPagesPerPage; index ++) { - int32 pageNumber = GetPageNumber(index); - if (pageNumber < 0) { - // no page at index - continue; - } - if (!IsPageLoaded(pageNumber)) { - LoadPage(pageNumber); - } - if (!IsPageValid()) { - // TODO show feedback - continue; - } - - BPoint scalingXY = GraphicsDriver::getScale(fNumberOfPagesPerPage, physicalRect, 100.0); - float scaling = min_c(scalingXY.x, scalingXY.y); - BPoint offset = GraphicsDriver::getOffset(fNumberOfPagesPerPage, index, fOrientation, &scalingXY, - scaledPhysicalRect, scaledPrintableRect, physicalRect); - - // draw page contents - PushState(); - - // print job coordinates are relative to the printable rect - SetScale(ZoomFactor() * scaling); - - BPoint leftTopMargin(BPoint(kPreviewLeftMargin, kPreviewTopMargin)); - leftTopMargin.x /= ZoomFactor() * scaling; - leftTopMargin.y /= ZoomFactor() * scaling; - offset += leftTopMargin; - SetOrigin(offset); - - // constrain clipping region to printable rectangle - BRect clipRect(fReader.PrintableRect()); - clipRect.OffsetTo(0, 0); - BRegion clip(clipRect); - ConstrainClippingRegion(&clip); - - fCachedPage->Draw(this); - - PopState(); - } -} +// #pragma mark - PreviewWindow -void PreviewView::Draw(BRect rect) { - if (fReader.InitCheck() == B_OK) { - DrawPageFrame(rect); - DrawPage(rect); - } -} -void PreviewView::FrameResized(float width, float height) { - FixScrollbars(); -} - -bool PreviewView::ShowsFirstPage() const { - return fPage == 0; -} - -bool PreviewView::ShowsLastPage() const { - return fPage == NumberOfPages() - 1; -} - -int PreviewView::NumberOfPages() const { - return fNumberOfPages; -} - -void PreviewView::ShowNextPage() { - if (!ShowsLastPage()) { - fPage ++; - Invalidate(); - } -} - -void PreviewView::ShowPrevPage() { - if (!ShowsFirstPage()) { - fPage --; - Invalidate(); - } -} - -void PreviewView::ShowFirstPage() { - if (!ShowsFirstPage()) { - fPage = 0; - Invalidate(); - } -} - -void PreviewView::ShowLastPage() { - if (!ShowsLastPage()) { - fPage = NumberOfPages()-1; - Invalidate(); - } -} - -void PreviewView::ShowFindPage(int page) { - page --; - - if (page < 0) { - page = 0; - } else if (page > (NumberOfPages()-1)) { - page = NumberOfPages()-1; - } - - fPage = (int32)page; - - Invalidate(); -} - -bool PreviewView::CanZoomIn() const { - return fZoom < 4; -} - -bool PreviewView::CanZoomOut() const { - return fZoom > -2; -} - -void PreviewView::ZoomIn() { - if (CanZoomIn()) { - fZoom ++; FixScrollbars(); - Invalidate(); - } -} - -void PreviewView::ZoomOut() { - if (CanZoomOut()) { - fZoom --; FixScrollbars(); - Invalidate(); - } -} - -void PreviewView::FixScrollbars() { - BRect frame = Bounds(); - BScrollBar * scroll; - float x, y; - float bigStep, smallStep; - float width = PageRect().Width() + kPreviewLeftMargin + kPreviewRightMargin; - float height = PageRect().Height() + kPreviewTopMargin + kPreviewBottomMargin; - x = width - frame.Width(); - if (x < 0.0) { - x = 0.0; - } - y = height - frame.Height(); - if (y < 0.0) { - y = 0.0; - } - - scroll = ScrollBar (B_HORIZONTAL); - scroll->SetRange (0.0, x); - scroll->SetProportion ((width - x) / width); - bigStep = frame.Width() - 2; - smallStep = bigStep / 10.; - scroll->SetSteps (smallStep, bigStep); - - scroll = ScrollBar (B_VERTICAL); - scroll->SetRange (0.0, y); - scroll->SetProportion ((height - y) / height); - bigStep = frame.Height() - 2; - smallStep = bigStep / 10.; - scroll->SetSteps (smallStep, bigStep); -} - -// Implementation of PreviewWindow - -PreviewWindow::PreviewWindow(BFile* jobFile, bool showOkAndCancelButtons) - : BlockingWindow(BRect(20, 24, 400, 600), "Preview", B_DOCUMENT_WINDOW, 0) +PreviewWindow::PreviewWindow(BFile* jobFile, bool showOkAndCancelButtons) + : BlockingWindow(BRect(20, 24, 400, 600), "Preview", B_TITLED_WINDOW, + B_ASYNCHRONOUS_CONTROLS) + , fButtonBarHeight(0.0) { - const float kButtonDistance = 15; - const float kPageTextDistance = 10; - const float kVerticalIndent = 7; - const float kHorizontalIndent = 20; - - float top = kVerticalIndent; - float left = kHorizontalIndent; - float width, height; - - BRect r = Frame(); - r.right = r.IntegerWidth() - B_V_SCROLL_BAR_WIDTH; r.left = 0; - r.bottom = r.IntegerHeight() - B_H_SCROLL_BAR_HEIGHT; r.top = 0; + BRect bounds(Bounds()); + bounds.OffsetBy(10.0, 10.0); - // add navigation and zoom buttons - fFirst = new BButton(BRect(left, top, left+10, top+10), "First", "First Page", new BMessage(MSG_FIRST_PAGE)); + fFirst = new BButton(bounds, "first", "First Page", new BMessage(MSG_FIRST_PAGE)); AddChild(fFirst); + fFirst->ResizeToPreferred(); - fPrev = new BButton(BRect(left, top, left+10, top+10), "Prev", "Previous Page", new BMessage(MSG_PREV_PAGE)); + bounds.OffsetBy(fFirst->Bounds().Width() + 10.0, 0.0); + fPrev = new BButton(bounds, "previous", "Previous Page", new BMessage(MSG_PREV_PAGE)); AddChild(fPrev); fPrev->ResizeToPreferred(); - // remember size of largest button - width = fPrev->Bounds().Width()+1; - height = fPrev->Bounds().Height()+1; - fFirst->ResizeTo(width, height); - left = fFirst->Frame().right + kButtonDistance; - - // move Previous Page button after First Page button - fPrev->MoveTo(left, top); - left = fPrev->Frame().right + kButtonDistance; - - fNext = new BButton(BRect(left, top, left+10, top+10), "Next", "Next Page", new BMessage(MSG_NEXT_PAGE)); + bounds.OffsetBy(fPrev->Bounds().Width() + 10.0, 0.0); + fNext = new BButton(bounds, "next", "Next Page", new BMessage(MSG_NEXT_PAGE)); AddChild(fNext); - fNext->ResizeTo(width, height); - left = fNext->Frame().right + kButtonDistance; - - fLast = new BButton(BRect(left, top, left+10, top+10), "Last", "Last Page", new BMessage(MSG_LAST_PAGE)); - AddChild(fLast); - fLast->ResizeTo(width, height); - left = fLast->Frame().right + kPageTextDistance; - - // page number text - fPageNumber = new BTextControl(BRect(left, top+3, left+10, top+10), "FindPage", "", "", new BMessage(MSG_FIND_PAGE)); - fPageNumber->SetAlignment(B_ALIGN_RIGHT, B_ALIGN_RIGHT); - int num; - for (num = 0; num <= 255; num++) { - fPageNumber->TextView()->DisallowChar(num); - } - for (num = 0; num <= 9; num++) { - fPageNumber->TextView()->AllowChar('0' + num); - } - fPageNumber->TextView()-> SetMaxBytes(5); - AddChild(fPageNumber); - fPageNumber->ResizeTo(be_plain_font->StringWidth("999999") + 10, height); - left = fPageNumber->Frame().right + 5; - - // number of pages text - fPageText = new BStringView(BRect(left, top, left + 100, top + 15), "pageText", ""); - AddChild(fPageText); - // estimate max. width - float pageTextWidth = fPageText->StringWidth("of 99999 Pages"); - // estimate height - BFont font; - fPageText->GetFont(&font); - float pageTextHeight = font.Size() + 2; - // resize to estimated size - fPageText->ResizeTo(pageTextWidth, pageTextHeight); - left += pageTextWidth + kPageTextDistance; - fPageText->MoveBy(0, (height - font.Size()) / 2); - - - fZoomIn = new BButton(BRect(left, top, left+10, top+10), "ZoomIn", "Zoom In", new BMessage(MSG_ZOOM_IN)); - AddChild(fZoomIn); - fZoomIn->ResizeTo(width, height); - left = fZoomIn->Frame().right + kButtonDistance; - - fZoomOut = new BButton(BRect(left, top, left+10, top+10), "ZoomOut", "Zoom Out", new BMessage(MSG_ZOOM_OUT)); - AddChild(fZoomOut); - fZoomOut->ResizeTo(width, height); + fNext->ResizeToPreferred(); + + bounds.OffsetBy(fNext->Bounds().Width() + 10.0, 0.0); + fLast = new BButton(bounds, "last", "Last Page", new BMessage(MSG_LAST_PAGE)); + AddChild(fLast); + fLast->ResizeToPreferred(); + + bounds = fLast->Frame(); + bounds.OffsetBy(fLast->Bounds().Width() + 10.0, 0.0); + fPageNumber = new BTextControl(bounds, "numOfPage", "99", "", + new BMessage(MSG_FIND_PAGE)); + AddChild(fPageNumber); + fPageNumber->ResizeToPreferred(); + fPageNumber->SetDivider(0.0); + fPageNumber->SetAlignment(B_ALIGN_RIGHT, B_ALIGN_RIGHT); + fPageNumber->MoveBy(0.0, bounds.Height() - fPageNumber->Bounds().Height()); + + uint32 num; + for (num = 0; num <= 255; num++) + fPageNumber->TextView()->DisallowChar(num); + + for (num = 0; num <= 9; num++) + fPageNumber->TextView()->AllowChar('0' + num); + fPageNumber->TextView()-> SetMaxBytes(5); + + bounds.OffsetBy(fPageNumber->Bounds().Width() + 5.0, 0.0); + fPageText = new BStringView(bounds, "pageText", ""); + AddChild(fPageText); + fPageText->ResizeTo(fPageText->StringWidth("of 99999 Pages"), + fFirst->Bounds().Height()); + + bounds.OffsetBy(fPageText->Bounds().Width() + 10.0, 0.0); + fZoomIn = new BButton(bounds, "zoomIn", "Zoom In", new BMessage(MSG_ZOOM_IN)); + AddChild(fZoomIn); + fZoomIn->ResizeToPreferred(); + + bounds.OffsetBy(fZoomIn->Bounds().Width() + 10.0, 0.0); + fZoomOut = new BButton(bounds, "ZoomOut", "Zoom Out", new BMessage(MSG_ZOOM_OUT)); + AddChild(fZoomOut); + fZoomOut->ResizeToPreferred(); + + fButtonBarHeight = fZoomOut->Frame().bottom + 10.0; + + bounds = Bounds(); + bounds.top = fButtonBarHeight; - fButtonBarHeight = fZoomOut->Frame().bottom + 7; - float bottomOfTopButtonBar = fButtonBarHeight; - if (showOkAndCancelButtons) { + // adjust preview height + bounds.bottom -= fButtonBarHeight; + // update the total height of both bars + fButtonBarHeight *= 2; + // cancel printing if user closes the preview window SetUserQuitResult(B_ERROR); - // add another button bar at bottom of window - r.bottom -= fButtonBarHeight; - // update the total height of both bars - fButtonBarHeight *= 2; - - // right align buttons - left = r.Width() - width - kHorizontalIndent; - top = r.Height() + kVerticalIndent + B_H_SCROLL_BAR_HEIGHT; - - left -= width + kButtonDistance; - BButton *printJob = new BButton(BRect(left, top+2, left+10, top+12), "PrintJob", "Print", new BMessage(MSG_PRINT_JOB), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM); - printJob->MakeDefault(true); + BButton *printJob = new BButton(BRect(), "printJob", "Print", + new BMessage(MSG_PRINT_JOB), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM); AddChild(printJob); - printJob->ResizeTo(width+4, height+4); + printJob->ResizeToPreferred(); + printJob->MoveTo(bounds.right - (printJob->Bounds().Width() + 10.0), + bounds.bottom + 10.0); - left += width + kButtonDistance; - BButton *cancelJob = new BButton(BRect(left, top, left+10, top+10), "CancelJob", "Cancel", new BMessage(MSG_CANCEL_JOB), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM); + BButton *cancelJob = new BButton(BRect(), "cancelJob", "Cancel", + new BMessage(MSG_CANCEL_JOB), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM); AddChild(cancelJob); - cancelJob->ResizeTo(width, height); - } - - - // add preview view - r.top = bottomOfTopButtonBar; - fPreview = new PreviewView(jobFile, r); - - - fPreviewScroller = new BScrollView("PreviewScroller", fPreview, B_FOLLOW_ALL, 0, true, true, B_FANCY_BORDER); + cancelJob->ResizeToPreferred(); + cancelJob->MoveTo(printJob->Frame().left - (10.0 + cancelJob->Bounds().Width()), + bounds.bottom + 10.0); + + printJob->MakeDefault(true); + } + + bounds.right -= B_V_SCROLL_BAR_WIDTH; + bounds.bottom -= B_H_SCROLL_BAR_HEIGHT; + + fPreview = new PreviewView(jobFile, bounds); + fPreviewScroller = new BScrollView("PreviewScroller", fPreview, B_FOLLOW_ALL, + B_FRAME_EVENTS, true, true, B_FANCY_BORDER); fPreviewScroller->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); AddChild(fPreviewScroller); - + if (fPreview->InitCheck() == B_OK) { - ResizeToPage(true); + _ResizeToPage(); fPreview->FixScrollbars(); - UpdateControls(); + _UpdateControls(); + fPreview->MakeFocus(true); } } -void PreviewWindow::ResizeToPage(bool allowShrinking) { + +void +PreviewWindow::MessageReceived(BMessage* m) +{ + switch (m->what) { + case MSG_FIRST_PAGE: + fPreview->ShowFirstPage(); + break; + + case MSG_NEXT_PAGE: + fPreview->ShowNextPage(); + break; + + case MSG_PREV_PAGE: + fPreview->ShowPrevPage(); + break; + + case MSG_LAST_PAGE: + fPreview->ShowLastPage(); + break; + + case MSG_FIND_PAGE: + fPreview->ShowFindPage(atoi(fPageNumber->Text())) ; + break; + + case MSG_ZOOM_IN: + fPreview->ZoomIn(); + break; + + case MSG_ZOOM_OUT: + fPreview->ZoomOut(); + break; + + case B_MODIFIERS_CHANGED: + fPreview->MouseMoved(BPoint(), B_INSIDE_VIEW, m); + break; + + case MSG_CANCEL_JOB: + Quit(B_ERROR); + break; + + case MSG_PRINT_JOB: + Quit(B_OK); + break; + + default: + BlockingWindow::MessageReceived(m); + return; + } + _UpdateControls(); +} + + +status_t +PreviewWindow::Go() +{ + status_t st = InitCheck(); + if (st == B_OK) + return BlockingWindow::Go(); + + be_app->SetCursor(B_HAND_CURSOR); + Quit(); + return st; +} + + +void +PreviewWindow::_ResizeToPage() + { BScreen screen; - BRect r(fPreview->ViewRect()); - float width, height; - float maxWidth, maxHeight, minWidth; + if (screen.Frame().right == 0.0) + return; + const float windowBorderWidth = 5; const float windowBorderHeight = 5; - - if (screen.Frame().right == 0.0) { - return; // invalid screen object - } - - width = r.Width() + 1 + B_V_SCROLL_BAR_WIDTH; - height = r.Height() + 1 + fButtonBarHeight + B_H_SCROLL_BAR_HEIGHT; - // dimensions so that window does not reach outside of screen - maxWidth = screen.Frame().Width() + 1 - windowBorderWidth - Frame().left; - maxHeight = screen.Frame().Height() + 1 - windowBorderHeight - Frame().top; + BRect rect(fPreview->ViewRect()); + float width = rect.Width() + 1 + B_V_SCROLL_BAR_WIDTH; + float height = rect.Height() + 1 + fButtonBarHeight + B_H_SCROLL_BAR_HEIGHT; + + rect = screen.Frame(); + // dimensions so that window does not reach outside of screen + float maxWidth = rect.Width() + 1 - windowBorderWidth - Frame().left; + float maxHeight = rect.Height() + 1 - windowBorderHeight - Frame().top; // width so that all buttons are visible - minWidth = fZoomOut->Frame().right + 10; - - if (width < minWidth) width = minWidth; + float minWidth = fZoomOut->Frame().right + 10; + if (width < minWidth) width = minWidth; if (width > maxWidth) width = maxWidth; if (height > maxHeight) height = maxHeight; - - if (!allowShrinking) { - if (Bounds().Width() > width) width = Bounds().Width(); - if (Bounds().Height() > height) height = Bounds().Height(); - } + ResizeTo(width, height); } -void PreviewWindow::UpdateControls() { + +void +PreviewWindow::_UpdateControls() +{ fFirst->SetEnabled(!fPreview->ShowsFirstPage()); fPrev->SetEnabled(!fPreview->ShowsFirstPage()); fNext->SetEnabled(!fPreview->ShowsLastPage()); fLast->SetEnabled(!fPreview->ShowsLastPage()); fZoomIn->SetEnabled(fPreview->CanZoomIn()); fZoomOut->SetEnabled(fPreview->CanZoomOut()); - - BString page; - page << fPreview->CurrentPage(); - fPageNumber->SetText(page.String()); - BString text; - text << "of " - << fPreview->NumberOfPages(); - if (fPreview->NumberOfPages() == 1) { - text << " Page"; - } else { - text << " Pages"; - } - fPageText->SetText(text.String());} + BString text; + text << fPreview->CurrentPage(); + fPageNumber->SetText(text.String()); -void PreviewWindow::MessageReceived(BMessage* m) { - switch (m->what) { - case MSG_FIRST_PAGE: - fPreview->ShowFirstPage(); - break; - case MSG_NEXT_PAGE: - fPreview->ShowNextPage(); - break; - case MSG_PREV_PAGE: - fPreview->ShowPrevPage(); - break; - case MSG_LAST_PAGE: - fPreview->ShowLastPage(); - break; - case MSG_FIND_PAGE: - fPreview->ShowFindPage(atoi(fPageNumber->Text())) ; - break; - case MSG_ZOOM_IN: - fPreview->ZoomIn(); - ResizeToPage(); - break; - case MSG_ZOOM_OUT: - fPreview->ZoomOut(); - ResizeToPage(); - break; - case MSG_CANCEL_JOB: - Quit(B_ERROR); - break; - case MSG_PRINT_JOB: - Quit(B_OK); - break; - - default: - inherited::MessageReceived(m); return; - } - UpdateControls(); -} - -status_t PreviewWindow::Go() { - status_t st = InitCheck(); - if (st == B_OK) { - return inherited::Go(); - } else { - Quit(); - } - return st; + text.SetTo("of "); + text << fPreview->NumberOfPages() << " Page"; + if (fPreview->NumberOfPages() > 1) + text.Append("s"); + fPageText->SetText(text.String()); }