File: View.cpp

package info (click to toggle)
paintlib 2.6.2-14
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,920 kB
  • ctags: 3,874
  • sloc: cpp: 25,209; sh: 10,605; ansic: 1,891; makefile: 120
file content (991 lines) | stat: -rw-r--r-- 26,489 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
/*
/--------------------------------------------------------------------
|
|      $Id: View.cpp,v 1.31 2004/09/11 12:41:37 uzadow Exp $
|       view class implementation
|
|      In the document/view model, this is where document rendering
|      (drawing in this case) occurs. Also handles palette issues.
|
|      Copyright (c) 1998 Bernard Delme
|
\--------------------------------------------------------------------
*/

#include "stdafx.h"
#include "piclook.h"
#include "doc.h"
#include "view.h"
#include "mainfrm.h"

#include "filterdialog.h"
#include "cropfilterdlg.h"
#include "ditherdialog.h"
#include "ThresholdDlg.h"
#include "palviewdlg.h"
#include "contrastdlg.h"
#include "intensitydlg.h"
#include "lightnessdlg.h"

#include "filter/PLFilterContrast.h"
#include "filter/PLFilterThreshold.h"
#include "filter/PLFilterCrop.h"
#include "filter/PLFilterResizeHamming.h"
#include "filter/PLFilterResizeBilinear.h"
#include "filter/PLFilterResizeBox.h"
#include "filter/PLFilterResizeGaussian.h"
#include "filter/PLFilterIntensity.h"
#include "filter/PLFilterLightness.h"
#include "filter/PLFilterGrayscale.h"
#include "filter/PLFilterVideoInvert.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

#ifdef OWND
#include <OwndDll.h>
#endif

//This is for banded printing
#define PRINT_BAND_SIZE 1000
#define PRINT_BAND_OVERLAP 100

/////////////////////////////////////////////////////////////////////////////
// CPLView

#ifdef USES_DRAWDIB
IMPLEMENT_DYNCREATE(CPLView, CScrollView)
#else
IMPLEMENT_DYNCREATE(CPLView, CZoomView)
#endif

#ifdef USES_DRAWDIB
BEGIN_MESSAGE_MAP(CPLView, CScrollView)
#else
BEGIN_MESSAGE_MAP(CPLView, CZoomView)
#endif
    //{{AFX_MSG_MAP(CPLView)
    ON_WM_ERASEBKGND()
    ON_WM_SIZE()
    ON_MESSAGE(WM_DOREALIZE, OnDoRealize)
    ON_UPDATE_COMMAND_UI(ID_VIEW_FITIMAGE, OnUpdateFitImage)
    ON_COMMAND(ID_VIEW_FITIMAGE, OnFitImage)
    ON_UPDATE_COMMAND_UI(ID_VIEW_DITHER, OnUpdateDither)
    ON_COMMAND(ID_VIEW_DITHER, OnDither)
    ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
    ON_WM_KEYDOWN()
    ON_COMMAND(ID_FILTER_BILINEAR, OnFilterBilinear)
    ON_COMMAND(ID_FILTER_BOX, OnFilterBox)
    ON_COMMAND(ID_FILTER_GAUSS, OnFilterGauss)
    ON_COMMAND(ID_FILTER_HAMMING, OnFilterHamming)
    ON_COMMAND(ID_FILTER_CROP, OnFilterCrop)
    ON_COMMAND(ID_FILTER_GRAYSCALE, OnFilterGrayscale)
    ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
    ON_WM_LBUTTONDOWN()
    ON_WM_MBUTTONDOWN()
    ON_WM_RBUTTONDOWN()
    ON_COMMAND(ID_WINDOW_FIT, OnSizeToFit)
    ON_WM_SETCURSOR()
    ON_UPDATE_COMMAND_UI(ID_ZOOM_MODE, OnUpdateZoomMode)
    ON_COMMAND(ID_ZOOM_MODE, OnZoomMode)
    ON_WM_PAINT()
    ON_WM_MOUSEMOVE()
	ON_COMMAND(ID_FILTER_THRESHOLD, OnFilterThreshold)
	ON_UPDATE_COMMAND_UI(ID_VIEW_SHOWPALETTE, OnUpdateViewShowpalette)
	ON_COMMAND(ID_VIEW_SHOWPALETTE, OnViewShowpalette)
	ON_COMMAND(ID_FILTER_CONTRAST, OnFilterContrast)
	ON_COMMAND(ID_FILTER_INTENSITY, OnFilterIntensity)
	ON_COMMAND(ID_FILTER_LIGHTNESS, OnFilterLightness)
	ON_UPDATE_COMMAND_UI(ID_FILTER_CONTRAST, OnUpdateTrueColFilter)
	ON_COMMAND(ID_FILTER_INVERT, OnFilterInvert)
    ON_COMMAND(ID_CONVERT_TO_8BPP, OnConvertTo8BPP)
	ON_UPDATE_COMMAND_UI(ID_FILTER_GAMMA, OnUpdateTrueColFilter)
	ON_UPDATE_COMMAND_UI(ID_FILTER_BILINEAR, OnUpdateTrueColFilter)
	ON_UPDATE_COMMAND_UI(ID_FILTER_HAMMING, OnUpdateTrueColFilter)
	ON_UPDATE_COMMAND_UI(ID_FILTER_BOX, OnUpdateTrueColFilter)
	ON_UPDATE_COMMAND_UI(ID_FILTER_GRAYSCALE, OnUpdateTrueColFilter)
	ON_UPDATE_COMMAND_UI(ID_FILTER_THRESHOLD, OnUpdateTrueColFilter)
	//}}AFX_MSG_MAP

    // Standard printing commands
    ON_COMMAND(ID_FILE_PRINT, Super::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, Super::OnFilePrintPreview)

    ON_UPDATE_COMMAND_UI(ID_BMPINFO, OnUpdateBmpInfo)
    ON_UPDATE_COMMAND_UI(ID_INDICATOR_PIXELX, OnUpdatePixelXIndicator)
    ON_UPDATE_COMMAND_UI(ID_INDICATOR_PIXELY, OnUpdatePixelYIndicator)
    ON_UPDATE_COMMAND_UI(ID_INDICATOR_ZRATIO, OnUpdateRatioIndicator)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPLView construction/destruction

CPLView::CPLView()
{
    m_bFit = false;    // use real graphics dimensions
    m_bDither = true;  // use vfw's DrawDib engine
    m_bZooming = false;

    m_curPoint = AfxGetApp()->LoadCursor(IDC_POINTCURSOR);
    m_curMove  = AfxGetApp()->LoadCursor(IDC_PULLCURSOR);
}


CPLView::~CPLView()
{
    if (m_curPoint != NULL) DestroyCursor(m_curPoint);
    if (m_curMove  != NULL) DestroyCursor(m_curMove );
}

/////////////////////////////////////////////////////////////////////////////
// CPLView drawing


void CPLView::OnDraw(CDC* pDC)
{
    CPLViewerDoc* pDoc = GetDocument();
    if ( ! pDoc ) return;

    PLWinBmpEx* pDib = pDoc->GetDib();
    if ( ! pDib ) return;

    // else, we have a bitmap to show!
	// CWaitCursor wc;

    CSize docsize = pDoc->GetDocSize();
    CRect rcImg(0,0,docsize.cx,docsize.cy);
    CRect rcDest;
    if (! pDC->IsPrinting())   // display DC
    {
	if (m_bFit)
	    GetClientRect( rcDest );
	else
	{
	    rcDest = rcImg = m_InvalidRect;
	    if ( m_InvalidRect.IsRectEmpty() )
		TRACE0( "CTiffView::OnDraw(): invalidated region is empty\n" );
	}
    }
    else  // printer device context
    {
        // get size of printer page (in pixels)
        int cxPage = pDC->GetDeviceCaps(HORZRES);
        int cyPage = pDC->GetDeviceCaps(VERTRES);
        // get printer pixels per inch
        int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
        int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);

        // create a rectangle which preserves the document's aspect ratio,
        // and fills the page taking into account the relative size of a
        // printed pixel (cyInch / cxInch).

        rcDest.top = rcDest.left = 0;
        rcDest.right = cxPage;
        rcDest.bottom = MulDiv(docsize.cy * cyInch, cxPage, docsize.cx * cxInch);
        if (rcDest.bottom > cyPage)
        {
            rcDest.bottom = cyPage;
            rcDest.right  = MulDiv(docsize.cx * cxInch, cyPage, docsize.cy * cyInch);
        }
	// TRACE("(%d,%d);(%d,%d,%d,%d)\n", cxInch, cyInch,
	//    rcDest.left, rcDest.top, rcDest.right, rcDest.bottom );
    }

#ifndef USE_BANDING
#ifdef USES_DRAWDIB
    pDib->DrawEx(pDC->GetSafeHdc(), &rcDest, &rcImg, NULL, false, m_bDither ? -1 : 0);
#else
    int nStretchMode = COLORONCOLOR;
    int nScreenBPP   = thisApp.GetScreenBPP();
    // when in 256-color mode, see how haltoning performs
    if ( m_bDither && nScreenBPP <= 8 && nScreenBPP < (int)pDib->GetBitsPerPixel() )
      nStretchMode = HALFTONE;

    pDib->DrawEx(
      pDC->GetSafeHdc(),
      &rcDest,
      &rcImg,
      NULL,	// default palette
      false,	// release in the background (until WM_QUERYNEWPALETTE)
              // adjust stretch mode to display color depth
	    nStretchMode);
#endif
#else
	//The following is the "striping" modification for HP printer
  //support of large images - Kal Krishnan.
	int iDestStripSize = (int)(((double)PRINT_BAND_SIZE*(double)rcDest.Height())/
                              (double)(rcImg.Height()));
	int iDestStripOverlap = (int)(((double)PRINT_BAND_OVERLAP*(double)rcDest.Height())/
                                (double)(rcImg.Height()));
	int iImgTop = rcImg.top;   // variable to hold the current
                             // position on source rect
	int iDestTop = rcDest.top; // same for destination
	int iDestBottom = rcDest.bottom;
	int iImgBottom = rcImg.bottom;
	for (; iDestTop < iDestBottom; iDestTop+=iDestStripSize-iDestStripOverlap)
	{
		rcDest.top = iDestTop;
		rcDest.bottom = iDestTop + iDestStripSize;
		rcImg.top = iImgTop;
		rcImg.bottom = rcImg.top + PRINT_STRIP_SIZE;
		if (rcDest.bottom > iDestBottom)	//last strip
		{
			rcDest.bottom = iDestBottom;
			rcImg.bottom = iImgBottom;
		}
		pDib->DrawEx(
			pDC->GetSafeHdc(),
			&rcDest,
      &rcImg,
      NULL,	// default palette
      false,	// release in the background (until WM_QUERYNEWPALETTE)
      nStretchMode); // adjust stretch mode to display color depth
		iImgTop+=PRINT_BAND_SIZE-PRINT_BAND_OVERLAP;
	}
#endif
}

/////////////////////////////////////////////////////////////////////////////
// CPLView printing

BOOL CPLView::OnPreparePrinting(CPrintInfo* pInfo)
{
    pInfo->SetMaxPage(1);
    // default preparation
    return DoPreparePrinting(pInfo);
}

// We shouldn't have to override this, but I noticed printing
// was broken when going through CZoomView...
void CPLView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
    if ( ! pDC->IsPrinting())
	Super::OnPrepareDC( pDC, pInfo );
    else
	CScrollView::OnPrepareDC( pDC, pInfo );
}

////////////////////////////////////////
//  panning helper class

class CHandTracker : public CRectTracker
{
    CPLView *m_pView;
public:
    CHandTracker(CPLView *pView);
    virtual void OnChangedRect(const CRect& rectOld);
    virtual void DrawTrackerRect(LPCRECT lpRect, CWnd* pWndClipTo, CDC* pDC, CWnd* pWnd)
	{ /* no-op ! */ }
};


CHandTracker::CHandTracker(CPLView *pView)
{
    ASSERT(pView != NULL);
    m_pView = pView;
    m_sizeMin = CSize(1, 1);
}


void CHandTracker::OnChangedRect(const CRect& rectOld)
{
    CPoint delta;
    // call the view to update scroll position
    delta.x = m_rect.Width()  - rectOld.Width();
    delta.y = m_rect.Height() - rectOld.Height();
    // m_pView->ViewDPtoLP( &delta );
    CPoint anchor = m_pView->GetScrollPosition();
    anchor -= delta;

    if (anchor.x < 0) anchor.x = 0;
    if (anchor.y < 0) anchor.y = 0;
    if (! (m_pView->GetStyle() & WS_VSCROLL)) anchor.x = 0;
    if (! (m_pView->GetStyle() & WS_HSCROLL)) anchor.x = 0;

    if ( delta.x || delta.y )
	m_pView->ScrollToPosition( anchor );
}


/////////////////////////////////////////////////////////////////////////////
// CPLView commands

/*
 *  Woosh, we got the WM_DOREALIZE message from MDI supervisor,
 *  time to shine our brightest colors
 */
LRESULT CPLView::OnDoRealize(WPARAM wParam, LPARAM)
{
    bool bRet = false;
    ASSERT(wParam != NULL);
    CPLViewerDoc* pDoc = GetDocument();
    if (pDoc->GetDib() == NULL)
        return bRet;  // must be a new document

    CPalette* pPal = CPalette::FromHandle( pDoc->GetDocPalette() );
    if (pPal != NULL)
    {
        CMainFrame* pAppFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
        ASSERT_KINDOF(CMainFrame, pAppFrame);

        CClientDC appDC(pAppFrame);
        // All views but one should be a background palette.
        // wParam contains a handle to the active view, so the SelectPalette
        // bForceBackground flag is false only if wParam == m_hWnd (this view)
        CPalette* oldPalette = appDC.SelectPalette(pPal, ((HWND)wParam) != m_hWnd);

        if (oldPalette != NULL)
        {
            UINT nColorsChanged = appDC.RealizePalette();
            if (nColorsChanged > 0)
            {
                //pDoc->UpdateAllViews(NULL);
                Invalidate(false);
                //UpdateWindow();
                bRet = true;
            }
            appDC.SelectPalette(oldPalette, true);
        }
        else
        {
            TRACE0("\tSelectPalette failed in CPLView::OnDoRealize()\n");
            //MessageBeep(0);
        }

    }

    return (LRESULT) bRet;
}

void CPLView::NotifyRanges()
{
#ifdef USES_DRAWDIB
    SetScrollSizes(MM_TEXT, GetDocument()->GetDocSize());
#else
    SetZoomSizes(GetDocument()->GetDocSize());
    SetZoomMode( MODE_ZOOMOFF );
    m_bZooming = false;
    SetZoomRatio(1.0);	// not adjusted by CZoomView ?!?
#endif
}

void CPLView::OnInitialUpdate()
{
    Super::OnInitialUpdate();
    ASSERT(GetDocument() != NULL);

    // set scrolling ranges
    NotifyRanges();

    // size window perfectly around frame
    GetParentFrame()->RecalcLayout();
    OnSizeToFit();
}

void CPLView::OnActivateView(bool bActivate, CView* pActivateView,
                             CView* pDeactiveView)
{
    Super::OnActivateView(bActivate, pActivateView, pDeactiveView);

    if (bActivate)
        //if (pDeactiveView != pActivateView)
    {
        // probably we switched from view/doc within MDI app,
        // so this view deserves foreground palette
        ASSERT(pActivateView == this);
        OnDoRealize((WPARAM)m_hWnd, 0);    // same as SendMessage(WM_DOREALIZE);
    }
}

BOOL CPLView::OnEraseBkgnd(CDC* pDC)
{
#ifdef USES_DRAWDIB
    if (m_bFit)
        // in this case, we cover the whole client area with our
        // drawing, so there's no need to erase
        return true;
#else
    if ((GetStyle() & WS_VSCROLL) &&
	(GetStyle() & WS_HSCROLL))  // page occupies whole view
	return true;
#endif
    else
        return Super::OnEraseBkgnd(pDC);
}


void CPLView::OnSize(UINT nType, int cx, int cy)
{
    Super::OnSize(nType, cx, cy);

    if (m_bFit)  // track new dimension, avoid scrollbars
        SetScaleToFitSize(CSize(cx,cy));
}


// In order to perform minimal redrawing, we need to capture
// the update area, which is unreachable in OnDraw()
void CPLView::OnPaint()
{
    if ( GetUpdateRect( m_InvalidRect ))
    {
	m_InvalidRect.NormalizeRect();
	// TRACE_RECT( m_InvalidRect );
#ifndef USES_DRAWDIB
	ViewDPtoLP( (LPPOINT) &m_InvalidRect, 2 );
#endif
	CRect docrect( CPoint(0,0), GetDocument()->GetDocSize() );
	m_InvalidRect.InflateRect( 1, 1 );	//?!?
	m_InvalidRect &= docrect;
    }
    else
	m_InvalidRect.SetRect( 0,0,0,0 );

    Super::OnPaint();
}

// user toggles the 'fit image to viewable area' button
void CPLView::OnFitImage()
{
    if (m_bFit = ! m_bFit)
    {
        CRect cr;
        GetClientRect( cr );
        SetScaleToFitSize(cr.Size());
    }
    else
        NotifyRanges();

    //pDoc->UpdateAllViews(NULL);
    Invalidate(false);
}

void CPLView::OnUpdateFitImage(CCmdUI* pCmdUI)
{
#ifndef USES_DRAWDIB
    pCmdUI->Enable(false);
#else
    pCmdUI->SetCheck(m_bFit);
#endif
}

void CPLView::OnUpdateViewShowpalette(CCmdUI* pCmdUI)
{
  PLWinBmpEx* pDib = GetDocument()->GetDib();
  if (pDib->GetBitsPerPixel() == 8)
    pCmdUI->Enable(true);
  else
    pCmdUI->Enable(false);
}

void CPLView::OnZoomMode()
{
#ifndef USES_DRAWDIB
    m_bZooming = ! m_bZooming;
    SetZoomMode(m_bZooming ? MODE_ZOOMIN : MODE_ZOOMOFF);
#endif
}

void CPLView::OnUpdateZoomMode(CCmdUI* pCmdUI)
{
#ifdef USES_DRAWDIB
    pCmdUI->Enable(false);
#else
    pCmdUI->SetCheck(m_bZooming);
#endif
}

// experiment GDI vs VFW DIB drawing

void CPLView::OnDither()
{
    m_bDither = ! m_bDither;
    Invalidate(false);
}

void CPLView::OnUpdateDither(CCmdUI* pCmdUI)
{
    pCmdUI->SetCheck(m_bDither);
}

void CPLView::OnEditCopy()
{
    PLWinBmpEx* pDib = GetDocument()->GetDib();
    pDib->ToClipboard();
}

void CPLView::OnEditPaste()
{
    PLWinBmpEx* pDib = GetDocument()->GetDib();
    pDib->FromClipboard();
    Invalidate(true);
    NotifyRanges();
}


// a Super has no keyboard interface, so let's roll our own
void CPLView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    switch( nChar )
    {
    case VK_HOME:
        SendMessage(::GetAsyncKeyState(VK_CONTROL) ?
                    WM_VSCROLL : WM_HSCROLL,SB_TOP);
        break;
    case VK_END:
        SendMessage(::GetAsyncKeyState(VK_CONTROL) ?
                    WM_VSCROLL : WM_HSCROLL,SB_BOTTOM);
        break;
    case VK_PRIOR:
        SendMessage(WM_VSCROLL,SB_PAGEUP);
        break;
    case VK_NEXT:
        SendMessage(WM_VSCROLL,SB_PAGEDOWN);
        break;
    case VK_UP:
        SendMessage(WM_VSCROLL,SB_LINEUP);
        break;
    case VK_DOWN:
        SendMessage(WM_VSCROLL,SB_LINEDOWN);
        break;
    case VK_LEFT:
        SendMessage(WM_HSCROLL,SB_LINEUP);
        break;
    case VK_RIGHT:
        SendMessage(WM_HSCROLL,SB_LINEDOWN);
        break;
    default:
        Super::OnKeyDown(nChar, nRepCnt, nFlags);
    }
}

void CPLView::OnUpdateTrueColFilter(CCmdUI* pCmdUI) 
{
  PLWinBmpEx* pDib = GetDocument()->GetDib();
  if (pDib->GetBitsPerPixel() < 24)
    pCmdUI->Enable (false);
  else
    pCmdUI->Enable (true);
}


void CPLView::OnFilterBilinear()
{
  PLFilterDialog Dlg (this, GetDocument()->GetDib(), "Bilinear Resize", false);
  if (Dlg.DoModal() == IDOK)
  {
    PLFilterResizeBilinear Filter (Dlg.GetWidth(), Dlg.GetHeight());
    applyFilterAndShow (&Filter);
  }
}

void CPLView::OnFilterBox()
{
  PLFilterDialog Dlg (this, GetDocument()->GetDib(), "Box filter", false);
  if (Dlg.DoModal() == IDOK)
  {
    PLFilterResizeBox Filter (Dlg.GetWidth(), Dlg.GetHeight());
    applyFilterAndShow (&Filter);
  }
}

void CPLView::OnFilterGauss()
{
  PLFilterDialog Dlg (this, GetDocument()->GetDib(), "Gaussian blur", true);
  if (Dlg.DoModal() == IDOK)
  {
    PLFilterResizeGaussian Filter (Dlg.GetWidth(), Dlg.GetHeight(), Dlg.GetRadius());
    applyFilterAndShow (&Filter);
  }
}

void CPLView::OnFilterHamming()
{
  PLFilterDialog Dlg (this, GetDocument()->GetDib(), "Hamming", true);
  if (Dlg.DoModal() == IDOK)
  {
    PLFilterResizeHamming Filter (Dlg.GetWidth(), Dlg.GetHeight(), Dlg.GetRadius());
    applyFilterAndShow (&Filter);
  }
}

void CPLView::OnFilterCrop()
{
  PLWinBmpEx* pDib = GetDocument()->GetDib();
  if (pDib->GetBitsPerPixel() == 1)
  {
      MessageBox ("This filter doesn't work for b/w bitmaps.", "piclook");
      return;
  }

  CCropFilterDlg Dlg(this);
  if (Dlg.DoModal() == IDOK)
  {
    PLFilterCrop Filter (Dlg.m_XMin, Dlg.m_YMin, Dlg.m_XMax, Dlg.m_YMax);
    applyFilterAndShow (&Filter);
  }
}

void CPLView::OnFilterGrayscale()
{
  CWaitCursor wc;
  PLWinBmpEx* pDib = GetDocument()->GetDib();

  pDib->ApplyFilter (PLFilterGrayscale());
  Invalidate(true);
  NotifyRanges();
}

void CPLView::OnFilterThreshold()
{
  CThresholdDlg Dlg(this);
  if (Dlg.DoModal() == IDOK)
  {
    PLFilterThreshold Filter (Dlg.GetMinThreshold(), Dlg.GetMaxThreshold(),Dlg.GetChannel());
    applyFilterAndShow (&Filter);
  }
}

void CPLView::OnFilterContrast() 
{
  CContrastDlg Dlg(this);
  if (Dlg.DoModal() == IDOK)
  {
    PLFilterContrast Filter (Dlg.GetContrast(), Dlg.GetOffset());
    applyFilterAndShow (&Filter);
  }
}

void CPLView::OnFilterIntensity() 
{
  CIntensityDlg Dlg(this);
  if (Dlg.DoModal() == IDOK)
  {
    PLFilterIntensity Filter (Dlg.m_Intensity, Dlg.m_Offset, Dlg.m_Exponent);
    applyFilterAndShow (&Filter);
  }
}

void CPLView::OnFilterLightness() 
{
  CLightnessDlg Dlg(this);
  if (Dlg.DoModal() == IDOK)
  {
    PLFilterLightness Filter (Dlg.m_Lightness);
    applyFilterAndShow (&Filter);
  }
}

void CPLView::OnFilterInvert() 
{
  PLFilterVideoInvert Filter;
  applyFilterAndShow (&Filter);
}

void CPLView::OnConvertTo8BPP()
{
  CDitherDialog Dlg(this);
  if (Dlg.DoModal() == IDOK)
  {
    CWaitCursor wc;

    GetDocument()->ConvertTo8BPP(Dlg.m_iDitherPaletteType, Dlg.m_iDitherType);
    InvalidateRect(NULL);
    NotifyRanges();
  }
}

void CPLView::OnViewShowpalette()
{
  PLWinBmpEx* pDib = GetDocument()->GetDib();
  CPalViewDlg Dlg (this, pDib->GetPalette());
  Dlg.DoModal();
}

BOOL CPLView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
    if (nHitTest != HTCLIENT)
        m_nDocPosX = m_nDocPosY = -1;

    //    if (GetZoomMode() == MODE_ZOOMOFF && nHitTest == HTCLIENT && IsMovable() )
    if (! m_bZooming && nHitTest == HTCLIENT && IsMovable() )
    {
        ::SetCursor(m_curPoint);
        return true;
    }
    else
        return Super::OnSetCursor(pWnd, nHitTest, message);
}


BOOL CPLView::IsMovable() const
{
    return (GetStyle() & (WS_VSCROLL | WS_HSCROLL));
}

void CPLView::OnLButtonDown(UINT nFlags, CPoint point)
{
    // if (GetZoomMode() == MODE_ZOOMOFF && IsMovable())
    if (! m_bZooming && IsMovable())
    {
	HCURSOR prvCur = ::SetCursor(m_curMove);

	if ( CHandTracker(this).TrackRubberBand(this, point) )
	    Invalidate(false);

	::SetCursor(prvCur);
    }
#ifndef USES_DRAWDIB
    else if (m_bZooming)
    {
	// emulate 3-button mouse with <Ctrl> key
	if ( nFlags & MK_CONTROL )
	    OnMButtonDown(nFlags, point);
	else
	    Super::OnLButtonDown(nFlags, point);
    }
#endif
}

void CPLView::OnRButtonDown(UINT nFlags, CPoint point)
{
#ifndef USES_DRAWDIB
    if (GetZoomMode() == MODE_ZOOMOFF)
	NotifyRanges();
    else
    {
	BOOL bWasZoomed = IsMovable();
	ViewDPtoLP( &point );
	DoZoomOut( &point );
    }
#endif
}

void CPLView::OnMButtonDown(UINT nFlags, CPoint point)
{
#ifndef USES_DRAWDIB
    if (GetZoomMode() != MODE_ZOOMOFF)
	DoZoomFull();
#ifdef OWND
    else if ( IsMovable() )
	StartPanning( this, GetStyle() & WS_VSCROLL, GetStyle() & WS_HSCROLL, point );
#endif	// OWND
#endif	// DRAWDIB
}

void CPLView::OnMouseMove(UINT nFlags, CPoint point)
{
    CPoint pt( point );
#ifndef USES_DRAWDIB
    ViewDPtoLP( &pt );
#endif
    CSize docsize = GetDocument()->GetDocSize();
    if ( pt.x >= 0 && pt.x < docsize.cx
	    && pt.y >= 0 && pt.y < docsize.cy )
    {
        m_nDocPosX = pt.x;
        m_nDocPosY = pt.y;
    }
    else
        m_nDocPosX = m_nDocPosY = -1;

    Super::OnMouseMove(nFlags, point);
}

void CPLView::OnUpdateBmpInfo(CCmdUI *pCmdUI)
{
  PLWinBmpEx* pDib = GetDocument()->GetDib();
  ASSERT( pDib );
  CString str;
  str.Format(" %d x %d; %d bpp; %d x %d dpi; %d kb",
             pDib->GetWidth(),
             pDib->GetHeight(),
             pDib->GetBitsPerPixel(),
             pDib->GetResolution().x,
             pDib->GetResolution().y,
             pDib->GetMemUsed() / 1024
            );
  pCmdUI->SetText( str );
}

void CPLView::OnUpdatePixelXIndicator(CCmdUI *pCmdUI)
{
    char cbuf[8] = "";
    if (m_nDocPosX >= 0) wsprintf( cbuf, "%5d", m_nDocPosX );
    pCmdUI->SetText( cbuf );
}


void CPLView::OnUpdatePixelYIndicator(CCmdUI *pCmdUI)
{
    char cbuf[8] = "";
    if (m_nDocPosY >= 0) wsprintf( cbuf, "%5d", m_nDocPosY );
    pCmdUI->SetText( cbuf );
}

void CPLView::OnUpdateRatioIndicator(CCmdUI *pCmdUI)
{
#ifndef USES_DRAWDIB
    char cbuf[8];
    double r = GetZoomRatio();
    if ( r >= 1.0 )
	sprintf( cbuf, "%.2f:1", r );
    else
	sprintf( cbuf, "1:%.2f", 1/r );
    pCmdUI->SetText( cbuf );
#endif
}

//////////////////
// Size To Fit command (courtesy of Paul DiLascia!)
//
void CPLView::OnSizeToFit()
{
    ResizeParentToFit(false);

    // MFC might have grown the window off the screen--I'll fix it
    CRect rc, rcMax;
    CFrameWnd* pFrame = GetParentFrame();
    ASSERT_VALID(pFrame);
    pFrame->GetWindowRect(&rc);
    CWnd* pGrandParent = pFrame->GetParent();
    if (pGrandParent)
    {
        // use top level window as maximum rectangle
        pGrandParent->ScreenToClient(&rc);
        pGrandParent->GetClientRect(&rcMax);
    }
    else
    {
        // use whole screen as maximum rectangle
        rcMax.SetRect(0,0,GetSystemMetrics(SM_CXSCREEN),
                      GetSystemMetrics(SM_CYSCREEN));
    }

    bool bTooBig = false;
    if (rc.bottom > rcMax.bottom)
    {
        rc.bottom = rcMax.bottom;
        rc.right += GetSystemMetrics(SM_CXVSCROLL) +
                    GetSystemMetrics(SM_CXBORDER);
        bTooBig = true;
    }
    if (rc.right > rcMax.right)
    {
        rc.right = rcMax.right;
        bTooBig = true;
    }
    if (bTooBig)
    {
        pFrame->SetWindowPos(NULL, 0, 0, rc.Width(), rc.Height(),
                             SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE);
    }
}


void CPLView::applyFilterAndShow (PLFilter * pFilter)
{
	PLWinBmpEx* pDib = GetDocument()->GetDib();
  CWaitCursor wc;
  pFilter->ApplyInPlace (pDib);
  Invalidate(true);
	NotifyRanges();
}

/*
/--------------------------------------------------------------------
|
|      $Log: View.cpp,v $
|      Revision 1.31  2004/09/11 12:41:37  uzadow
|      removed plstdpch.h
|
|      Revision 1.30  2002/03/29 19:18:23  uzadow
|      Fixed bug in crop dialog.
|
|      Revision 1.29  2002/02/24 13:00:56  uzadow
|      Documentation update; removed buggy PLFilterRotate.
|
|      Revision 1.28  2001/10/21 17:12:40  uzadow
|      Added PSD decoder beta, removed BPPWanted from all decoders, added PLFilterPixel.
|
|      Revision 1.27  2001/10/16 17:12:27  uzadow
|      Added support for resolution information (Luca Piergentili)
|
|      Revision 1.26  2001/09/30 16:57:26  uzadow
|      Improved speed of 2passfilter.h, code readability changes.
|
|      Revision 1.25  2001/09/16 19:03:23  uzadow
|      Added global name prefix PL, changed most filenames.
|
|      Revision 1.24  2001/01/15 15:05:32  uzadow
|      Added CBmp::ApplyFilter() and CBmp::CreateFilteredCopy()
|
|      Revision 1.23  2000/12/18 22:42:53  uzadow
|      Replaced RGBAPIXEL with PLPixel32.
|
|      Revision 1.22  2000/12/05 00:00:06  uzadow
|      Added lightness dialog.
|
|      Revision 1.21  2000/11/06 23:22:52  uzadow
|      Added dialogs for Contrast and Intensity
|
|      Revision 1.20  2000/10/23 21:13:29  uzadow
|      Removed Filter
|
|      Revision 1.19  2000/10/12 21:59:34  uzadow
|      Added CreateFromHDIBBitmap() and CopyPalette() to PLWinBmp
|      Added CF_DIB support to PLWinBmp::FromClipboard() (Richard Hollis)
|
|      Revision 1.18  2000/09/26 14:28:47  Administrator
|      Added Threshold filter
|
|      Revision 1.17  2000/09/26 12:14:50  Administrator
|      Refactored quantization.
|
|      Revision 1.16  2000/07/11 17:18:54  Ulrich von Zadow
|      Added support for banded printing.
|
|      Revision 1.15  2000/07/07 14:00:26  Ulrich von Zadow
|      Minor bugfixes.
|
|      Revision 1.14  2000/05/28 11:09:56  Ulrich von Zadow
|      no message
|
|      Revision 1.13  2000/03/31 12:20:07  Ulrich von Zadow
|      Video invert filter (beta)
|
|      Revision 1.12  2000/03/31 11:53:32  Ulrich von Zadow
|      Added quantization support.
|
|      Revision 1.11  2000/03/30 21:52:16  Ulrich von Zadow
|      Minor bugfix.
|
|      Revision 1.10  2000/03/30 21:47:41  Ulrich von Zadow
|      Added zoom-in mode, PLWinBmpEx, conditional use of DrawDIB
|      and some other nice stuff by Bernard Delme.
|
|      Revision 1.8  2000/01/10 23:53:02  Ulrich von Zadow
|      Changed formatting & removed tabs.
|
|      Revision 1.7  1999/12/30 15:54:48  Ulrich von Zadow
|      Added PLWinBmp::FromClipBoard() and CreateFromHBitmap().
|
|      Revision 1.6  1999/12/02 17:07:36  Ulrich von Zadow
|      Changes by bdelmee.
|
|      Revision 1.5  1999/10/21 18:48:18  Ulrich von Zadow
|      no message
|
|      Revision 1.4  1999/10/21 16:07:06  Ulrich von Zadow
|      Moved filters to separate directory. Added Crop, Grayscale and
|      GetAlpha filters.
|
|      Revision 1.3  1999/10/19 21:33:49  Ulrich von Zadow
|      Added filter support.
|
|
\--------------------------------------------------------------------
*/