home *** CD-ROM | disk | FTP | other *** search
- // CPdfPageView.cpp
- //
- // Copyright 1999 Sander van der Wal
- //
- // $Id: pdfpageview.cpp 1.2 2000-09-25 21:00:59+02 svdwal Exp svdwal $
- //
- // $Log: pdfpageview.cpp $
- // Revision 1.2 2000-09-25 21:00:59+02 svdwal
- // Profile macro's removed
- //
- // Revision 1.1 2000-09-17 13:18:13+02 svdwal
- // Initial checkin
- //
-
- #include "PdfPageView.h"
-
- // CONE
- #ifndef __COEMAIN_H__
- #include <coemain.h>
- #endif
-
- // EIKON
- #ifndef __EIKENV_H__
- #include <eikenv.h>
- #endif
- #ifndef __EIKMSG_H__
- #include <eikmsg.h>
- #endif
-
- // PDF
- #include "Object.h"
- #include "Catalog.h"
- #include "Page.h"
- #include "XRef.h"
- #include "PDFDoc.h"
- #include "FontEncoding.h"
-
- #include "PdfPageControl.h"
-
- #include "Pdf.hrh"
- #include "Pdf.rsg"
-
-
- // PROFILING
- #include "PROFILE.h"
-
- // Locals
-
- // zoom factors
- const TInt CPdfPageView::KZoomPage = -1;
- const TInt CPdfPageView::KZoomWidth= -2;
-
- static inline TInt round(double x) { return TInt(x+0.5); }
-
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // TPdfPageViewLayout
- //
- // Calculates and contains the positions and sizes of the control and scrolbars
- //
- //////////////////////////////////////////////////////////////////////////////
- TPdfPageViewLayout::TPdfPageViewLayout()
- : iControlRect()
- {}
-
-
- TPdfPageViewLayout::TPdfPageViewLayout(const TRect& aParentRect, const TSize& aChildSize, TInt aShowHBar, TInt aShowVBar)
- {
- iVisArea = aParentRect.Size();
-
- // first determine which scrollbars we need.
- TInt hBarHeight = 0;
- TInt vBarWidth = 0;
-
- if (aShowHBar == 0)
- iShowHBar = EFalse;
- else if (aShowHBar == 1)
- iShowHBar = ETrue;
- else // auto: we need a horizontal bar if the bitmap does not fit the view width
- iShowHBar = (aChildSize.iWidth > iVisArea.iWidth);
- if (iShowHBar)
- hBarHeight = CEikScrollBar::EScrollbarWidth;
-
- if (aShowVBar == 0)
- iShowVBar = EFalse;
- else if (aShowHBar == 1)
- iShowVBar = ETrue;
- else // auto: we need a vertical bar if the bitmap does not fit the view height
- iShowVBar = (aChildSize.iHeight > iVisArea.iHeight);
- if (iShowVBar)
- vBarWidth = CEikScrollBar::EScrollbarWidth;
-
- iVisArea.iWidth -= vBarWidth;
- iVisArea.iHeight-= hBarHeight;
-
- // calculate position and size of the control
-
- // if the width of the bitmap is small enough to fit, center it
- if (aChildSize.iWidth < iVisArea.iWidth) {
- iControlRect.iTl.iX =(iVisArea.iWidth - aChildSize.iWidth)/2;
- iControlRect.SetWidth(aChildSize.iWidth);
- }
- else {
- iControlRect.iTl.iX = 0;
- iControlRect.SetWidth(iVisArea.iWidth);
- }
-
- // if the height of the bitmap is small enough to fit, center it
- if (aChildSize.iHeight < iVisArea.iHeight) {
- iControlRect.iTl.iY =(iVisArea.iHeight - aChildSize.iHeight)/2;
- iControlRect.SetHeight(aChildSize.iHeight);
- }
- else {
- iControlRect.iTl.iY = 0;
- iControlRect.SetHeight(iVisArea.iHeight);
- }
-
- iHBarPos = iVisArea.AsPoint();
- iHBarPos.iX = 0;
- iHBarLength = Max(iControlRect.Size().iWidth, iVisArea.iWidth);
-
- iVBarPos = iVisArea.AsPoint();
- iVBarPos.iY = 0;
- iVBarLength = Max(iControlRect.Size().iHeight, iVisArea.iHeight);
- }
-
-
- TPdfPageViewLayout::TPdfPageViewLayout(const TPdfPageViewLayout& aLayout)
- : iControlRect(aLayout.iControlRect), iShowHBar(aLayout.iShowHBar), iShowVBar(aLayout.iShowVBar)
- {}
-
-
- TBool operator!=(const TPdfPageViewLayout& x, const TPdfPageViewLayout& y)
- {
- return (x.iControlRect!=y.iControlRect ||
- x.iShowHBar!=y.iShowHBar ||
- x.iShowVBar!=y.iShowVBar);
- }
-
-
- //////////////////////////////////////////////////////////////////////////////
- //
- // CPdfPageView
- //
- //////////////////////////////////////////////////////////////////////////////
- void CPdfPageView::ConstructL(const TRect& aRect)
- {
- CreateWindowL();
- Window().SetShadowDisabled(ETrue);
- EnableDragEvents();
-
- iContext=this;
- iBrushStyle=CGraphicsContext::ESolidBrush;
- iBrushColor=KRgbGray;
-
- SetRectL(aRect);
-
- ActivateL();
- }
-
-
- CPdfPageView::~CPdfPageView()
- {
- delete iControl;
- delete iHBar;
- delete iVBar;
- }
-
-
- void CPdfPageView::SetCmdHandler(MPdfPageViewCmdHandler* aCmdHandler)
- {
- iCmdHandler = aCmdHandler;
- }
-
-
- void CPdfPageView::SetModelL(PDFDoc* aDoc)
- {
- // clear up old model stuff
- DeleteComponentControls();
-
- // create new model stuff
- iDoc=aDoc;
- if (iDoc)
- CreateComponentControlsL();
- else
- DrawNow();
- }
-
-
- void CPdfPageView::SetModelSize(TSize aSizeInPixels)
- {
- iHBarModel.iScrollSpan = aSizeInPixels.iWidth;
- iVBarModel.iScrollSpan = aSizeInPixels.iHeight;
-
- SetSpanSize();
- }
-
-
- void CPdfPageView::SetSpanSize()
- {
- TSize vwSize = iLayout.ControlRect().Size();
-
- iHBarModel.iThumbSpan = Min(vwSize.iWidth, iHBarModel.iScrollSpan);
- iHBarModel.CheckBounds();
-
- iVBarModel.iThumbSpan = Min(vwSize.iHeight, iVBarModel.iScrollSpan);
- iVBarModel.CheckBounds();
-
- if (iControl)
- iControl->SetVisAreaOffset(ScrollOffset());
- }
-
-
- void CPdfPageView::SetScrollOffset(const TPoint& aScrollOffset)
- {
- TBool doHBar = iHBarModel.iThumbPosition != aScrollOffset.iX;
- if (doHBar) {
- iHBarModel.iThumbPosition = aScrollOffset.iX;
- iHBarModel.CheckBounds();
- if (iHBar)
- iHBar->SetModelThumbPosition(iHBarModel.iThumbPosition);
- }
-
- TBool doVBar = iVBarModel.iThumbPosition != aScrollOffset.iY;
- if (doVBar) {
- iVBarModel.iThumbPosition = aScrollOffset.iY;
- iVBarModel.CheckBounds();
- if (iVBar)
- iVBar->SetModelThumbPosition(iVBarModel.iThumbPosition);
- }
-
- if (iControl && (doHBar || doVBar)) {
- iControl->SetVisAreaOffset(ScrollOffset());
- iControl->DrawNow();
- }
- }
-
-
- TPoint CPdfPageView::ScrollOffset() const
- {
- return TPoint(iHBarModel.iThumbPosition, iVBarModel.iThumbPosition);
- }
-
-
- void CPdfPageView::CreateComponentControlsL()
- {
- iControl=new(ELeave) CPdfPageControl(iDoc);
- iControl->ConstructL();
- iControl->SetContainerWindowL(*this);
-
- iLayout = TPdfPageViewLayout(Size(), iControl->PageSizeInPixels(), iHBarVisibility, iVBarVisibility);
- iHBarModel.iThumbPosition = 0;
- iVBarModel.iThumbPosition = 0;
- SetSpanSize();
- iControl->SetRectL(iLayout.ControlRect());
- iControl->ActivateL();
-
- if (iLayout.ShowHBar())
- iHBar = CreateScrollBarL(CEikScrollBar::EHorizontal, iLayout.HBarPos(), iLayout.HBarLength(), &iHBarModel);
-
- if (iLayout.ShowVBar())
- iVBar = CreateScrollBarL(CEikScrollBar::EVertical, iLayout.VBarPos(), iLayout.VBarLength(), &iVBarModel);
-
- DrawNow();
- }
-
-
- void CPdfPageView::UpdateComponentControlsL()
- {
- // set the controls to the (possibly) new layout
- SetModelSize(iControl->PageSizeInPixels());
-
- // remember the current layout
- TPdfPageViewLayout oldLayout = iLayout;
-
- iLayout = TPdfPageViewLayout(Size(), iControl->PageSizeInPixels(), iHBarVisibility, iVBarVisibility);
- SetSpanSize();
- iControl->SetRectL(iLayout.ControlRect());
-
- // place the scrollbars
-
- if ((!iHBar && iLayout.ShowHBar()) ||
- oldLayout.HBarLength()!= iLayout.HBarLength()||
- oldLayout.HBarPos() != iLayout.HBarPos() ) {
- if (!iLayout.ShowHBar()){
- delete iHBar;
- iHBar = 0;
- }
- else if (!iHBar)
- iHBar = CreateScrollBarL(CEikScrollBar::EHorizontal, iLayout.HBarPos(), iLayout.HBarLength(), &iHBarModel);
- else {
- if (oldLayout.HBarPos() != iLayout.HBarPos())
- iHBar->SetPosition(iLayout.HBarPos());
- if (oldLayout.HBarLength()!= iLayout.HBarLength())
- iHBar->SetLengthAndModelL(iLayout.HBarLength(), &iHBarModel);
- }
- }
- if ((!iVBar && iLayout.ShowVBar()) ||
- oldLayout.VBarLength()!= iLayout.VBarLength()||
- oldLayout.VBarPos() != iLayout.VBarPos() ) {
- if (!iLayout.ShowVBar()) {
- delete iVBar;
- iVBar = 0;
- }
- else if (!iVBar)
- iVBar = CreateScrollBarL(CEikScrollBar::EVertical, iLayout.VBarPos(), iLayout.VBarLength(), &iVBarModel);
- else {
- if (oldLayout.VBarPos() != iLayout.VBarPos())
- iVBar->SetPosition(iLayout.VBarPos());
- if (oldLayout.VBarLength()!= iLayout.VBarLength())
- iVBar->SetLengthAndModelL(iLayout.VBarLength(), &iVBarModel);
- }
- }
-
- if (oldLayout.ControlRect() != iLayout.ControlRect())
- DrawDeferred(); // to clear up old parts of the bitmap and/or scrollbars
- }
-
-
- void CPdfPageView::DeleteComponentControls()
- {
- delete iControl;
- iControl = 0;
-
- delete iHBar;
- iHBar = 0;
-
- delete iVBar;
- iVBar = 0;
- }
-
-
- CEikScrollBar* CPdfPageView::CreateScrollBarL(CEikScrollBar::TOrientation aOrientation, const TPoint& aPos, TInt aLength, TEikScrollBarModel* aModel)
- {
- TInt flags = 0;
-
- CEikScrollBar* bar = new(ELeave) CEikScrollBar;
- CleanupStack::PushL(bar);
- bar->ConstructL(this, this, aOrientation, 0, flags);
- bar->SetPosition(aPos);
- bar->SetLengthAndModelL(aLength, aModel);
- bar->ActivateL();
- CleanupStack::Pop();
- return bar;
- }
-
-
- void CPdfPageView::HandlePointerEventL(const TPointerEvent& aPointerEvent)
- {
- if (aPointerEvent.iType == TPointerEvent::EButton1Down &&
- iControl &&
- iControl->Rect().Contains(aPointerEvent.iPosition)) {
-
- TReal usrX, usrY;
- iControl->CvtDevToUser(aPointerEvent.iPosition + ScrollOffset(), usrX, usrY);
-
- iCmdHandler->CmdTapL(TPoint(round(usrX*KTwipsPerPoint), round(usrY*KTwipsPerPoint)));
- }
- }
-
-
- TKeyResponse CPdfPageView::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
- {
- TKeyResponse res = EKeyWasConsumed;
- if (aType == EEventKey) {
- switch (aKeyEvent.iCode) {
- case EKeyEscape:
- iCmdHandler->CmdCancelL();
- break;
-
- case EKeyHome:
- if (aKeyEvent.iModifiers & EModifierCtrl)
- iCmdHandler->CmdFirstPageL();
- else
- HandleLeftRightMove(-3);
- break;
-
- case EKeyEnd:
- if (aKeyEvent.iModifiers & EModifierCtrl)
- iCmdHandler->CmdLastPageL();
- else
- HandleLeftRightMove(3);
- break;
-
- case EKeyPageUp:
- if ((aKeyEvent.iModifiers & EModifierCtrl) ||
- (iRotate == 0 && iVBarModel.iThumbPosition==0) ||
- (iRotate == 90 && iHBarModel.iThumbPosition>=iHBarModel.MaxThumbPos()) ||
- (iRotate == 180 && iVBarModel.iThumbPosition>=iVBarModel.MaxThumbPos()) ||
- (iRotate == 270 && iHBarModel.iThumbPosition==0))
- iCmdHandler->CmdPrevPageL(EFalse);
- else
- HandlePrevNextMove(-2);
- break;
-
- case ' ':
- case EKeyPageDown:
- if ((aKeyEvent.iCode == EKeyPageDown) && (aKeyEvent.iModifiers & EModifierCtrl) ||
- (iRotate == 0 && iVBarModel.iThumbPosition>=iVBarModel.MaxThumbPos()) ||
- (iRotate == 90 && iHBarModel.iThumbPosition==0) ||
- (iRotate == 180 && iVBarModel.iThumbPosition==0) ||
- (iRotate == 270 && iHBarModel.iThumbPosition>=iHBarModel.MaxThumbPos()))
- iCmdHandler->CmdNextPageL();
- else
- HandlePrevNextMove(2);
- break;
-
- case EKeyLeftArrow:
- HandleLeftRightMove(aKeyEvent.iModifiers & EModifierCtrl? -2:-1);
- break;
-
- case EKeyRightArrow:
- HandleLeftRightMove(aKeyEvent.iModifiers & EModifierCtrl? 2: 1);
- break;
-
- case EKeyUpArrow:
- HandleUpDownMove(aKeyEvent.iModifiers & EModifierCtrl ? -2:-1);
- break;
-
- case EKeyDownArrow:
- HandleUpDownMove(aKeyEvent.iModifiers & EModifierCtrl ? 2: 1);
- break;
-
- default:
- res = EKeyWasNotConsumed;
- break;
- } // switch
- }
- else
- res = EKeyWasNotConsumed;
- return res;
- }
-
-
- void CPdfPageView::HandleScrollEventL(CEikScrollBar* aScrollBar, TEikScrollEvent aEvent)
- {
- switch (aEvent) {
- case EEikScrollEnd: HandleLeftRightMove( 3); break;
- case EEikScrollPageRight: HandleLeftRightMove( 2); break;
- case EEikScrollRight: HandleLeftRightMove( 1); break;
- case EEikScrollLeft: HandleLeftRightMove(-1); break;
- case EEikScrollPageLeft: HandleLeftRightMove(-2); break;
- case EEikScrollHome: HandleLeftRightMove(-3); break;
-
- case EEikScrollBottom: HandleUpDownMove( 3); break;
- case EEikScrollPageDown: HandleUpDownMove( 2); break;
- case EEikScrollDown: HandleUpDownMove( 1); break;
- case EEikScrollUp: HandleUpDownMove(-1); break;
- case EEikScrollPageUp: HandleUpDownMove(-2); break;
- case EEikScrollTop: HandleUpDownMove(-3); break;
-
- default:
- if (aEvent & KEikScrollEventBarMask==KEikScrollEventFromHBar) {
- iHBarModel = *(aScrollBar->Model());
- iHBarModel.CheckBounds();
- }
- else {
- iVBarModel = *(aScrollBar->Model());
- iVBarModel.CheckBounds();
- }
- if (iControl) {
- iControl->SetVisAreaOffset(ScrollOffset());
- iControl->DrawNow();
- }
- break;
- }
- }
-
-
- void CPdfPageView::HandlePrevNextMove(TInt aDir)
- {
- switch (iRotate) {
- case 180: aDir = -aDir;
- // falltrough
- case 0: HandleUpDownMove(aDir);
- break;
-
- case 90: aDir = -aDir;
- // fallthrough
- case 270: HandleLeftRightMove(aDir);
- break;
-
- default:
- break;
- }
- }
-
-
- void CPdfPageView::HandleUpDownMove(TInt aDir)
- {
- switch (aDir) {
- case -3: // top of page
- case 3: // bottom of page
- iVBarModel.iThumbPosition = aDir < 0 ? 0 : iVBarModel.MaxThumbPos();
- break;
-
- case -2: // prev part
- case 2: // next part
- iVBarModel.iThumbPosition += aDir * iVBarModel.iThumbSpan*9/20;
- break;
-
- case -1: // prev line
- case 1: // next line
- iVBarModel.iThumbPosition += aDir * iVBarModel.iThumbSpan/20;
- break;
-
- default:
- break;
- }
-
- iVBarModel.CheckBounds();
- if (iVBar)
- iVBar->SetModelThumbPosition(iVBarModel.iThumbPosition);
- if (iControl) {
- iControl->SetVisAreaOffset(ScrollOffset());
- iControl->DrawNow();
- }
- }
-
-
- void CPdfPageView::HandleLeftRightMove(TInt aDir)
- {
- switch (aDir) {
- case -3: // top of page
- case 3: // bottom of page
- iHBarModel.iThumbPosition = aDir < 0 ? 0 : iHBarModel.MaxThumbPos();
- break;
-
- case -2: // prev part
- case 2: // next part
- iHBarModel.iThumbPosition += aDir * iHBarModel.iThumbSpan/10;
- break;
-
- case -1: // prev line
- case 1: // next line
- iHBarModel.iThumbPosition += aDir * iHBarModel.iThumbSpan/40;
- break;
-
- default:
- break;
- }
-
- iHBarModel.CheckBounds();
- if (iHBar)
- iHBar->SetModelThumbPosition(iHBarModel.iThumbPosition);
- if (iControl) {
- iControl->SetVisAreaOffset(ScrollOffset());
- iControl->DrawNow();
- }
- }
-
-
- TInt CPdfPageView::CountComponentControls() const
- {
- return (iControl ? 1 : 0);
- }
-
-
- CCoeControl* CPdfPageView::ComponentControl(TInt aIndex) const
- {
- return (aIndex == 0 ? iControl : 0);
- }
-
-
- void CPdfPageView::Draw(const TRect& /* aRect */) const
- {
- CWindowGc& gc=SystemGc();
- gc.SetPenStyle(CGraphicsContext::ENullPen);
- gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
- gc.DrawRect(Rect());
- }
-
-
- void CPdfPageView::SizeChangedL()
- {
- if (iControl)
- UpdateComponentControlsL();
- }
-
-
- void CPdfPageView::ScrollToTop()
- {
- if (iRotate == 0 || iRotate == 180)
- HandleUpDownMove(iRotate == 180 ? 3: -3);
- else
- HandleLeftRightMove(iRotate == 90 ? 3: -3);
- }
-
-
- void CPdfPageView::ScrollToBottom()
- {
- if (iRotate == 0 || iRotate == 180)
- HandleUpDownMove(iRotate == 180 ? -3 : 3);
- else
- HandleLeftRightMove(iRotate == 90 ? -3 : 3);
- }
-
-
- void CPdfPageView::StartRenderingL(TInt aPage, const TPdfPageViewMemento& aMemento)
- {
- SetDpi(aPage, aMemento.iZoom, aMemento.iRotate);
-
- iControl->StartRenderingL(aPage, iDpi, aMemento);
-
- // if we get here, we can update the display
- iRotate = aMemento.iRotate;
- UpdateComponentControlsL();
- }
-
-
- void CPdfPageView::CancelRendering()
- {
- if (iControl)
- iControl->CancelRendering();
- }
-
-
- void CPdfPageView::SetScrollBarVisibility(TInt aHBarVisibility, TInt aVBarVisibility)
- {
- TInt oldH = iHBarVisibility;
- TInt oldV = iVBarVisibility;
- iHBarVisibility = aHBarVisibility;
- iVBarVisibility = aVBarVisibility;
- if (iHBarVisibility != oldH || iVBarVisibility != oldV)
- SizeChangedL();
- }
-
-
- TPoint CPdfPageView::Center() const
- {
- TPoint devCenter(ScrollOffset().iX+iLayout.HBarLength()/2,
- ScrollOffset().iY+iLayout.VBarLength()/2);
-
- TReal usrX, usrY;
- iControl->CvtDevToUser(devCenter, usrX, usrY);
-
- TPoint usrPos(round(usrX*KTwipsPerPoint), round(usrY*KTwipsPerPoint));
- return usrPos;
- }
-
-
- void CPdfPageView::SetCenter(TPoint aPosition)
- {
- TPoint devPos;
- iControl->CvtUserToDev(aPosition.iX/KTwipsPerPoint, aPosition.iY/KTwipsPerPoint, devPos);
-
- SetScrollOffset(TPoint(devPos.iX-iLayout.HBarLength()/2,
- devPos.iY-iLayout.VBarLength()/2));
- }
-
-
- void CPdfPageView::SetSelection(const TRect& aSelection)
- {
- if (iControl) {
- TRect devSel;
- iControl->CvtUserToDev(aSelection.iTl.iX/KTwipsPerPoint, aSelection.iTl.iY/KTwipsPerPoint, devSel.iTl);
- iControl->CvtUserToDev(aSelection.iBr.iX/KTwipsPerPoint, aSelection.iBr.iY/KTwipsPerPoint, devSel.iBr);
- devSel.Normalize();
- iControl->SetSelection(devSel);
-
- if (!devSel.IsEmpty())
- SetScrollOffset(TPoint(devSel.Center().iX-iLayout.HBarLength()/2,
- devSel.Center().iY-iLayout.VBarLength()/2));
- }
- }
-
-
- void CPdfPageView::SetDpi(TInt aPage, TInt aZoom, TInt aRotate)
- {
- // calculate the page size in dots per inch
- if (aZoom == KZoomPage) {
- TInt hDpi, vDpi;
- if (aRotate == 90 || aRotate == 270) {
- hDpi = TInt((iLayout.VisAreaWidth() /iDoc->getPageHeight(aPage))*KPointsPerInch);
- vDpi = TInt((iLayout.VisAreaHeight()/iDoc->getPageWidth(aPage)) *KPointsPerInch);
- }
- else {
- hDpi = round((iLayout.VisAreaWidth() /iDoc->getPageWidth(aPage)) *KPointsPerInch);
- vDpi = round((iLayout.VisAreaHeight()/iDoc->getPageHeight(aPage))*KPointsPerInch);
- }
- iDpi = (hDpi < vDpi) ? hDpi : vDpi;
- }
- else if (aZoom == KZoomWidth) {
- if (aRotate == 90 || aRotate == 270)
- iDpi = TInt(iLayout.VisAreaWidth()/iDoc->getPageHeight(aPage)*KPointsPerInch);
- else
- iDpi = TInt(iLayout.VisAreaWidth()/iDoc->getPageWidth(aPage) *KPointsPerInch);
- }
- else
- iDpi = aZoom * 72 /100;
- }
-
-
- TInt CPdfPageView::RealZoom()
- {
- return iDpi * 100 / 72;
- }