xref: /trunk/main/fpicker/source/win32/filepicker/PreviewCtrl.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_fpicker.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir //------------------------------------------------------------------------
32*cdf0e10cSrcweir // includes
33*cdf0e10cSrcweir //------------------------------------------------------------------------
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <tchar.h>
36*cdf0e10cSrcweir #include "PreviewCtrl.hxx"
37*cdf0e10cSrcweir #include <osl/diagnose.h>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #if defined _MSC_VER
40*cdf0e10cSrcweir #pragma warning(push, 1)
41*cdf0e10cSrcweir #endif
42*cdf0e10cSrcweir #include <windows.h>
43*cdf0e10cSrcweir #if defined _MSC_VER
44*cdf0e10cSrcweir #pragma warning(pop)
45*cdf0e10cSrcweir #endif
46*cdf0e10cSrcweir #include <ocidl.h>
47*cdf0e10cSrcweir #include <olectl.h>
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir //------------------------------------------------------------------------
50*cdf0e10cSrcweir // defines
51*cdf0e10cSrcweir //------------------------------------------------------------------------
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir #define PREVIEWWND_CLASS_NAME TEXT("PreviewWnd###")
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir #define HIMETRIC_INCH 2540
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir // means 3 pixel left and 3 pixel right
58*cdf0e10cSrcweir #define HORZ_BODER_SPACE    6
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir // means 3 pixel top and 3 pixel bottom
61*cdf0e10cSrcweir #define VERT_BORDER_SPACE   6
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir //---------------------------------------------------
64*cdf0e10cSrcweir // static member initialization
65*cdf0e10cSrcweir //---------------------------------------------------
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir CFilePreview* CFilePreview::s_FilePreviewInst = NULL;
68*cdf0e10cSrcweir CFilePreview::FILEPREVIEW_SINGLETON_DESTROYER_T CFilePreview::s_SingletonDestroyer;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir //---------------------------------------------------
71*cdf0e10cSrcweir // some useful helper functions
72*cdf0e10cSrcweir //---------------------------------------------------
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir namespace // private
75*cdf0e10cSrcweir {
76*cdf0e10cSrcweir     class CPreviewException
77*cdf0e10cSrcweir     {
78*cdf0e10cSrcweir         // used when registering or creation
79*cdf0e10cSrcweir         // of the preview window failed
80*cdf0e10cSrcweir     };
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     //------------------------------------------------------------
83*cdf0e10cSrcweir     //
84*cdf0e10cSrcweir     //------------------------------------------------------------
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir     inline
87*cdf0e10cSrcweir     sal_Int32 SubDiv( sal_Int32 nNumber, sal_Int32 nMinuend, sal_Int32 nDenominator )
88*cdf0e10cSrcweir     {
89*cdf0e10cSrcweir         return ( static_cast<sal_Int32>( ( nNumber - nMinuend ) / nDenominator ) );
90*cdf0e10cSrcweir     }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     //------------------------------------------------------------
93*cdf0e10cSrcweir     // convert himetric to pixel
94*cdf0e10cSrcweir     //------------------------------------------------------------
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     inline
97*cdf0e10cSrcweir     sal_Int32 Himetric2Pixel( HDC hDC, sal_Int32 hmSize, sal_Int32 nIndex )
98*cdf0e10cSrcweir     {
99*cdf0e10cSrcweir         return MulDiv( hmSize, GetDeviceCaps( hDC, nIndex), HIMETRIC_INCH );
100*cdf0e10cSrcweir     }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     //------------------------------------------------------------
103*cdf0e10cSrcweir     //
104*cdf0e10cSrcweir     //------------------------------------------------------------
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir     inline
107*cdf0e10cSrcweir     sal_uInt32 _getWidthRect( const RECT& aRect )
108*cdf0e10cSrcweir     {
109*cdf0e10cSrcweir         return ( aRect.right - aRect.left );
110*cdf0e10cSrcweir     }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir     //------------------------------------------------------------
113*cdf0e10cSrcweir     //
114*cdf0e10cSrcweir     //------------------------------------------------------------
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     inline
117*cdf0e10cSrcweir     sal_uInt32 _getHeightRect( const RECT& aRect )
118*cdf0e10cSrcweir     {
119*cdf0e10cSrcweir         return ( aRect.bottom - aRect.top );
120*cdf0e10cSrcweir     }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir     //------------------------------------------------------------
123*cdf0e10cSrcweir     // calc the upper left corner so that a given window will be
124*cdf0e10cSrcweir     // displayed centered within the given window
125*cdf0e10cSrcweir     //------------------------------------------------------------
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir     inline
128*cdf0e10cSrcweir     POINT _calcULCorner( HWND hwnd, const CDimension& aPicSize )
129*cdf0e10cSrcweir     {
130*cdf0e10cSrcweir         RECT rect;
131*cdf0e10cSrcweir         GetClientRect( hwnd, &rect );
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir         sal_Int32 nWidthWnd  = _getWidthRect( rect );
134*cdf0e10cSrcweir         sal_Int32 nHeightWnd = _getHeightRect( rect );
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         POINT ulCorner;
137*cdf0e10cSrcweir         ulCorner.x = SubDiv( nWidthWnd,  aPicSize.m_cx, 2 );
138*cdf0e10cSrcweir         ulCorner.y = SubDiv( nHeightWnd, aPicSize.m_cy, 2 );
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir         return ulCorner;
141*cdf0e10cSrcweir     }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     //------------------------------------------------------------
144*cdf0e10cSrcweir     // test if a picture with the given dimensions fits into an
145*cdf0e10cSrcweir     // arbitrary window
146*cdf0e10cSrcweir     // we expect the width and height to be in pixel
147*cdf0e10cSrcweir     //------------------------------------------------------------
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir     inline
150*cdf0e10cSrcweir     sal_Bool _pictureSizeFitsWindowSize( HWND hwnd, const CDimension& aPicSize )
151*cdf0e10cSrcweir     {
152*cdf0e10cSrcweir         RECT rect;
153*cdf0e10cSrcweir         GetClientRect( hwnd, &rect );
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir         sal_Int32 nWidthWnd  = _getWidthRect( rect );
156*cdf0e10cSrcweir         sal_Int32 nHeightWnd = _getHeightRect( rect );
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir         return ( ( ( nWidthWnd  - HORZ_BODER_SPACE )  >= aPicSize.m_cx ) &&
159*cdf0e10cSrcweir                  ( ( nHeightWnd - VERT_BORDER_SPACE ) >= aPicSize.m_cy ) );
160*cdf0e10cSrcweir     }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     //------------------------------------------------------------
163*cdf0e10cSrcweir     // calc the dimemsions so that a given picture fits into a
164*cdf0e10cSrcweir     // given window, if the picture fits into the given window
165*cdf0e10cSrcweir     // the original CDimension will be returned
166*cdf0e10cSrcweir     //------------------------------------------------------------
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     inline
169*cdf0e10cSrcweir     CDimension _scalePictureSize( HWND hwnd, const CDimension& aPicSize )
170*cdf0e10cSrcweir     {
171*cdf0e10cSrcweir         CDimension scaledPicSize = aPicSize;
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir         if ( !_pictureSizeFitsWindowSize( hwnd, aPicSize ) )
174*cdf0e10cSrcweir         {
175*cdf0e10cSrcweir             RECT rect;
176*cdf0e10cSrcweir             GetClientRect( hwnd, &rect );
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir             // the dimensions of the preview wnd are not equal
179*cdf0e10cSrcweir             // that's why we equalize it
180*cdf0e10cSrcweir             sal_Int32 nHeightWnd = _getHeightRect( rect ) - VERT_BORDER_SPACE;
181*cdf0e10cSrcweir             sal_Int32 nWidthWnd  = nHeightWnd;
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir             if ( aPicSize.m_cx >= aPicSize.m_cy )
184*cdf0e10cSrcweir             {
185*cdf0e10cSrcweir                 scaledPicSize.m_cx = nWidthWnd;
186*cdf0e10cSrcweir                 scaledPicSize.m_cy =
187*cdf0e10cSrcweir                     static_cast< sal_Int32 >(
188*cdf0e10cSrcweir                         aPicSize.m_cy * nWidthWnd / aPicSize.m_cx );
189*cdf0e10cSrcweir             }
190*cdf0e10cSrcweir             else
191*cdf0e10cSrcweir             {
192*cdf0e10cSrcweir                 scaledPicSize.m_cx =
193*cdf0e10cSrcweir                     static_cast< sal_Int32 >(
194*cdf0e10cSrcweir                         aPicSize.m_cx * nHeightWnd / aPicSize.m_cy );
195*cdf0e10cSrcweir                 scaledPicSize.m_cy = nHeightWnd;
196*cdf0e10cSrcweir             }
197*cdf0e10cSrcweir         }
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir         return scaledPicSize;
200*cdf0e10cSrcweir     }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir } // end namespace
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir //---------------------------------------------------
206*cdf0e10cSrcweir // to ensure only one instance (singleton)
207*cdf0e10cSrcweir //---------------------------------------------------
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir CFilePreview* CFilePreview::createInstance(
210*cdf0e10cSrcweir     HWND aParent,
211*cdf0e10cSrcweir     POINT ulCorner,
212*cdf0e10cSrcweir     const CDimension& aSize,
213*cdf0e10cSrcweir     HINSTANCE hInstance,
214*cdf0e10cSrcweir     sal_Bool bShow,
215*cdf0e10cSrcweir     sal_Bool bEnabled )
216*cdf0e10cSrcweir {
217*cdf0e10cSrcweir     if ( !s_FilePreviewInst )
218*cdf0e10cSrcweir     {
219*cdf0e10cSrcweir         try
220*cdf0e10cSrcweir         {
221*cdf0e10cSrcweir             s_FilePreviewInst = new CFilePreview(
222*cdf0e10cSrcweir                 aParent, ulCorner, aSize, hInstance, bShow, bEnabled );
223*cdf0e10cSrcweir             s_SingletonDestroyer.reset( s_FilePreviewInst );
224*cdf0e10cSrcweir         }
225*cdf0e10cSrcweir         catch( CPreviewException& )
226*cdf0e10cSrcweir         {
227*cdf0e10cSrcweir             OSL_ASSERT( !s_FilePreviewInst );
228*cdf0e10cSrcweir             OSL_ENSURE( sal_False, "Creation of the preview window failed" );
229*cdf0e10cSrcweir         }
230*cdf0e10cSrcweir         catch( CAutoOleInit::COleInitException& )
231*cdf0e10cSrcweir         {
232*cdf0e10cSrcweir             OSL_ASSERT( !s_FilePreviewInst );
233*cdf0e10cSrcweir             OSL_ENSURE( sal_False, "OleInitalize failed" );
234*cdf0e10cSrcweir         }
235*cdf0e10cSrcweir     }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir     return s_FilePreviewInst;
238*cdf0e10cSrcweir }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir //---------------------------------------------------
241*cdf0e10cSrcweir //
242*cdf0e10cSrcweir //---------------------------------------------------
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir CFilePreview::CFilePreview(
245*cdf0e10cSrcweir     HWND aParent,
246*cdf0e10cSrcweir     POINT ulCorner,
247*cdf0e10cSrcweir     const CDimension& aSize,
248*cdf0e10cSrcweir     HINSTANCE hInstance,
249*cdf0e10cSrcweir     sal_Bool bShow,
250*cdf0e10cSrcweir     sal_Bool bEnabled ) :
251*cdf0e10cSrcweir     m_hInstance( hInstance ),
252*cdf0e10cSrcweir     m_bEnabled( bEnabled )
253*cdf0e10cSrcweir {
254*cdf0e10cSrcweir     // register the preview window class
255*cdf0e10cSrcweir     WNDCLASSEX wndClsEx;
256*cdf0e10cSrcweir     ZeroMemory(&wndClsEx, sizeof(wndClsEx));
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir     wndClsEx.cbSize        = sizeof(wndClsEx);
259*cdf0e10cSrcweir     wndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
260*cdf0e10cSrcweir     wndClsEx.lpfnWndProc   = CFilePreview::WndProc;
261*cdf0e10cSrcweir     wndClsEx.hInstance     = m_hInstance;
262*cdf0e10cSrcweir     wndClsEx.hbrBackground = (HBRUSH)( COLOR_INACTIVEBORDER + 1 );
263*cdf0e10cSrcweir     wndClsEx.lpszClassName = PREVIEWWND_CLASS_NAME;
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir     // register the preview window class
266*cdf0e10cSrcweir     // !!! Win95 -   the window class will be unregistered automaticly
267*cdf0e10cSrcweir     //               if the dll is unloaded
268*cdf0e10cSrcweir     //     Win2000 - the window class must be unregistered manually
269*cdf0e10cSrcweir     //               if the dll is unloaded
270*cdf0e10cSrcweir     m_atomPrevWndClass = RegisterClassEx(&wndClsEx);
271*cdf0e10cSrcweir     if ( !m_atomPrevWndClass )
272*cdf0e10cSrcweir         throw CPreviewException( );
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir     // create the preview window in invisible state
275*cdf0e10cSrcweir     sal_uInt32 dwStyle = bShow ? (WS_CHILD | WS_VISIBLE) : WS_CHILD;
276*cdf0e10cSrcweir     m_hwnd = CreateWindowEx(
277*cdf0e10cSrcweir         WS_EX_CLIENTEDGE,
278*cdf0e10cSrcweir         PREVIEWWND_CLASS_NAME,
279*cdf0e10cSrcweir         TEXT(""),
280*cdf0e10cSrcweir         dwStyle,
281*cdf0e10cSrcweir         ulCorner.x,
282*cdf0e10cSrcweir         ulCorner.y,
283*cdf0e10cSrcweir         aSize.m_cx,
284*cdf0e10cSrcweir         aSize.m_cy,
285*cdf0e10cSrcweir         aParent,
286*cdf0e10cSrcweir         (HMENU)100, // for child windows this will
287*cdf0e10cSrcweir                     // be used as child window identifier
288*cdf0e10cSrcweir         m_hInstance,
289*cdf0e10cSrcweir         0 );
290*cdf0e10cSrcweir     if (!IsWindow(m_hwnd))
291*cdf0e10cSrcweir         throw CPreviewException( );
292*cdf0e10cSrcweir }
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir //---------------------------------------------------
295*cdf0e10cSrcweir //
296*cdf0e10cSrcweir //---------------------------------------------------
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir CFilePreview::~CFilePreview( )
299*cdf0e10cSrcweir {
300*cdf0e10cSrcweir     // unregister preview window class
301*cdf0e10cSrcweir     sal_Bool bRet = UnregisterClass(
302*cdf0e10cSrcweir         (LPCTSTR)MAKELONG( m_atomPrevWndClass, 0 ),
303*cdf0e10cSrcweir         m_hInstance );
304*cdf0e10cSrcweir     OSL_POSTCOND( bRet, "Unregister preview window class failed" );
305*cdf0e10cSrcweir }
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir //---------------------------------------------------
308*cdf0e10cSrcweir // sets the size of the preview window
309*cdf0e10cSrcweir //---------------------------------------------------
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir sal_Bool SAL_CALL CFilePreview::setSize( const CDimension& aSize )
312*cdf0e10cSrcweir {
313*cdf0e10cSrcweir     OSL_PRECOND( IsWindow( m_hwnd ), "Preview window not initialized" );
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir     // resize the fileopen file listbox
316*cdf0e10cSrcweir     return SetWindowPos(
317*cdf0e10cSrcweir         m_hwnd,
318*cdf0e10cSrcweir         NULL,
319*cdf0e10cSrcweir         0,
320*cdf0e10cSrcweir         0,
321*cdf0e10cSrcweir         aSize.m_cx,
322*cdf0e10cSrcweir         aSize.m_cy,
323*cdf0e10cSrcweir         SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
324*cdf0e10cSrcweir }
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir //---------------------------------------------------
327*cdf0e10cSrcweir // returns the dimension of the preview
328*cdf0e10cSrcweir //---------------------------------------------------
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir sal_Bool SAL_CALL CFilePreview::getSize( CDimension& theSize ) const
331*cdf0e10cSrcweir {
332*cdf0e10cSrcweir     OSL_PRECOND( IsWindow( m_hwnd ), "Preview window not initialized" );
333*cdf0e10cSrcweir 
334*cdf0e10cSrcweir     RECT rect;
335*cdf0e10cSrcweir     sal_Bool bRet = GetWindowRect( m_hwnd, &rect );
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir     theSize.m_cx = _getWidthRect( rect );
338*cdf0e10cSrcweir     theSize.m_cy = _getHeightRect( rect );
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir     return bRet;
341*cdf0e10cSrcweir }
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir //---------------------------------------------------
344*cdf0e10cSrcweir // sets the position of the upper left corner
345*cdf0e10cSrcweir // of the preview window relative to the
346*cdf0e10cSrcweir // upper left corner of the parent window
347*cdf0e10cSrcweir //---------------------------------------------------
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir sal_Bool SAL_CALL CFilePreview::setPos( POINT ulCorner )
350*cdf0e10cSrcweir {
351*cdf0e10cSrcweir     OSL_PRECOND( IsWindow( m_hwnd ), "Preview window not initialized" );
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir     // resize the fileopen file listbox
354*cdf0e10cSrcweir     return SetWindowPos(
355*cdf0e10cSrcweir         m_hwnd,
356*cdf0e10cSrcweir         NULL,
357*cdf0e10cSrcweir         ulCorner.x,
358*cdf0e10cSrcweir         ulCorner.y,
359*cdf0e10cSrcweir         0,
360*cdf0e10cSrcweir         0,
361*cdf0e10cSrcweir         SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
362*cdf0e10cSrcweir }
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir //---------------------------------------------------
365*cdf0e10cSrcweir // returns the current position of the preview
366*cdf0e10cSrcweir // relative to the upper left corner of the
367*cdf0e10cSrcweir // parent window
368*cdf0e10cSrcweir //---------------------------------------------------
369*cdf0e10cSrcweir 
370*cdf0e10cSrcweir sal_Bool SAL_CALL CFilePreview::getPos( POINT& ulCorner ) const
371*cdf0e10cSrcweir {
372*cdf0e10cSrcweir     OSL_PRECOND( IsWindow( m_hwnd ), "Preview window not initialized" );
373*cdf0e10cSrcweir 
374*cdf0e10cSrcweir     POINT pt = { 0, 0 };
375*cdf0e10cSrcweir     RECT rect;
376*cdf0e10cSrcweir 
377*cdf0e10cSrcweir     sal_Bool bRet = GetWindowRect( m_hwnd, &rect );
378*cdf0e10cSrcweir 
379*cdf0e10cSrcweir     ulCorner.x = rect.left;
380*cdf0e10cSrcweir     ulCorner.y = rect.top;
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir     ScreenToClient( m_hwnd, &ulCorner );
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir     return bRet;
385*cdf0e10cSrcweir }
386*cdf0e10cSrcweir 
387*cdf0e10cSrcweir //---------------------------------------------------
388*cdf0e10cSrcweir //
389*cdf0e10cSrcweir //---------------------------------------------------
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir void SAL_CALL CFilePreview::enable( sal_Bool bEnable )
392*cdf0e10cSrcweir {
393*cdf0e10cSrcweir     m_bEnabled = bEnable;
394*cdf0e10cSrcweir 
395*cdf0e10cSrcweir     // force a redraw
396*cdf0e10cSrcweir     InvalidateRect( m_hwnd, NULL, sal_True );
397*cdf0e10cSrcweir     UpdateWindow( m_hwnd );
398*cdf0e10cSrcweir }
399*cdf0e10cSrcweir 
400*cdf0e10cSrcweir //---------------------------------------------------
401*cdf0e10cSrcweir // shows the preview window
402*cdf0e10cSrcweir // possible values see SHOW_STATE
403*cdf0e10cSrcweir // SS_SHOW     - make the window visible
404*cdf0e10cSrcweir // SS_HIDE     - hide the window
405*cdf0e10cSrcweir // SS_ENABLED  - enable the window
406*cdf0e10cSrcweir // SS_DISABLED - disable the window
407*cdf0e10cSrcweir //---------------------------------------------------
408*cdf0e10cSrcweir 
409*cdf0e10cSrcweir sal_Bool SAL_CALL CFilePreview::show( sal_Bool bShow )
410*cdf0e10cSrcweir {
411*cdf0e10cSrcweir     OSL_PRECOND( IsWindow( m_hwnd ), "Preview window not initialized" );
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir     sal_Int32 showState = bShow ? SW_SHOW : SW_HIDE;
414*cdf0e10cSrcweir     return ShowWindow( m_hwnd, showState );
415*cdf0e10cSrcweir }
416*cdf0e10cSrcweir 
417*cdf0e10cSrcweir //---------------------------------------------------
418*cdf0e10cSrcweir // if the preview is shown and enabled
419*cdf0e10cSrcweir // preview of the given file will be shown
420*cdf0e10cSrcweir // returns true on success or false if an error
421*cdf0e10cSrcweir // occured (the file in not there or not accessible etc.)
422*cdf0e10cSrcweir //---------------------------------------------------
423*cdf0e10cSrcweir 
424*cdf0e10cSrcweir sal_Bool SAL_CALL CFilePreview::update( const rtl::OUString& aFileName )
425*cdf0e10cSrcweir {
426*cdf0e10cSrcweir     OSL_PRECOND( IsWindow( m_hwnd ), "Preview window not initialized" );
427*cdf0e10cSrcweir 
428*cdf0e10cSrcweir     try
429*cdf0e10cSrcweir     {
430*cdf0e10cSrcweir         if ( m_bEnabled )
431*cdf0e10cSrcweir         {
432*cdf0e10cSrcweir             if ( m_IPicture )
433*cdf0e10cSrcweir                 m_IPicture.Release( );
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir             loadFile( aFileName );
436*cdf0e10cSrcweir 
437*cdf0e10cSrcweir             // force a complete window redraw
438*cdf0e10cSrcweir             InvalidateRect( m_hwnd, NULL, sal_True );
439*cdf0e10cSrcweir             UpdateWindow( m_hwnd );
440*cdf0e10cSrcweir         }
441*cdf0e10cSrcweir     }
442*cdf0e10cSrcweir     catch( _com_error& )
443*cdf0e10cSrcweir     {
444*cdf0e10cSrcweir     }
445*cdf0e10cSrcweir 
446*cdf0e10cSrcweir     return sal_True;
447*cdf0e10cSrcweir }
448*cdf0e10cSrcweir 
449*cdf0e10cSrcweir //---------------------------------------------------
450*cdf0e10cSrcweir //
451*cdf0e10cSrcweir //---------------------------------------------------
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir void SAL_CALL CFilePreview::onPaint( HWND hWnd, HDC hDC )
454*cdf0e10cSrcweir {
455*cdf0e10cSrcweir     OSL_PRECOND( IsWindow( m_hwnd ), "Preview window not initialized" );
456*cdf0e10cSrcweir 
457*cdf0e10cSrcweir     try
458*cdf0e10cSrcweir     {
459*cdf0e10cSrcweir         if ( m_bEnabled )
460*cdf0e10cSrcweir         {
461*cdf0e10cSrcweir             // get width and height of picture
462*cdf0e10cSrcweir             long cxPicHIMETRIC;
463*cdf0e10cSrcweir             long cyPicHIMETRIC;
464*cdf0e10cSrcweir 
465*cdf0e10cSrcweir             m_IPicture->get_Width( &cxPicHIMETRIC );
466*cdf0e10cSrcweir             m_IPicture->get_Height( &cyPicHIMETRIC );
467*cdf0e10cSrcweir 
468*cdf0e10cSrcweir             // convert himetric to pixels
469*cdf0e10cSrcweir             int cxPicPIXEL = Himetric2Pixel( hDC, cxPicHIMETRIC, LOGPIXELSX );
470*cdf0e10cSrcweir             int cyPicPIXEL = Himetric2Pixel( hDC, cyPicHIMETRIC, LOGPIXELSY );
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir             // scale the picture based on the size of the preview window
473*cdf0e10cSrcweir             RECT rcPrevWnd;
474*cdf0e10cSrcweir             GetClientRect(hWnd, &rcPrevWnd);
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir             CDimension scaledPicSize = _scalePictureSize(
477*cdf0e10cSrcweir                 hWnd, CDimension( cxPicPIXEL, cyPicPIXEL ) );
478*cdf0e10cSrcweir 
479*cdf0e10cSrcweir             // calc the upper left corner so that the picture
480*cdf0e10cSrcweir             // is centered within the window
481*cdf0e10cSrcweir             POINT ulCorner = _calcULCorner( hWnd, scaledPicSize );
482*cdf0e10cSrcweir 
483*cdf0e10cSrcweir             // render the picture
484*cdf0e10cSrcweir             HRESULT hr = m_IPicture->Render(
485*cdf0e10cSrcweir                 hDC,
486*cdf0e10cSrcweir                 ulCorner.x,
487*cdf0e10cSrcweir                 ulCorner.y,
488*cdf0e10cSrcweir                 scaledPicSize.m_cx,
489*cdf0e10cSrcweir                 scaledPicSize.m_cy,
490*cdf0e10cSrcweir                 0,
491*cdf0e10cSrcweir                 cyPicHIMETRIC,
492*cdf0e10cSrcweir                 cxPicHIMETRIC,
493*cdf0e10cSrcweir                 -cyPicHIMETRIC,
494*cdf0e10cSrcweir                 &rcPrevWnd );
495*cdf0e10cSrcweir         } // end if ( m_bEnabled )
496*cdf0e10cSrcweir     }
497*cdf0e10cSrcweir     catch( _com_error& )
498*cdf0e10cSrcweir     {
499*cdf0e10cSrcweir     }
500*cdf0e10cSrcweir }
501*cdf0e10cSrcweir 
502*cdf0e10cSrcweir //---------------------------------------------------
503*cdf0e10cSrcweir //
504*cdf0e10cSrcweir //---------------------------------------------------
505*cdf0e10cSrcweir 
506*cdf0e10cSrcweir sal_Bool CFilePreview::loadFile( const rtl::OUString& aFileName )
507*cdf0e10cSrcweir {
508*cdf0e10cSrcweir     HANDLE      hFile   = 0;
509*cdf0e10cSrcweir     HGLOBAL     hGlobal = 0;
510*cdf0e10cSrcweir     LPVOID      pData   = NULL;
511*cdf0e10cSrcweir     IStreamPtr  pIStream;
512*cdf0e10cSrcweir     HRESULT     hr = E_FAIL;
513*cdf0e10cSrcweir     sal_Bool    bRet;
514*cdf0e10cSrcweir     sal_uInt32  nBytesRead;
515*cdf0e10cSrcweir     sal_uInt32  fszExtra;
516*cdf0e10cSrcweir     sal_uInt32  fsize;
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir     hFile = CreateFile(
519*cdf0e10cSrcweir         aFileName.getStr( ),
520*cdf0e10cSrcweir         GENERIC_READ,
521*cdf0e10cSrcweir         0,
522*cdf0e10cSrcweir         NULL,
523*cdf0e10cSrcweir         OPEN_EXISTING,
524*cdf0e10cSrcweir         0,
525*cdf0e10cSrcweir         NULL );
526*cdf0e10cSrcweir     if ( INVALID_HANDLE_VALUE == hFile )
527*cdf0e10cSrcweir         goto CLEANUP_AND_EXIT;
528*cdf0e10cSrcweir 
529*cdf0e10cSrcweir     fszExtra = 0;
530*cdf0e10cSrcweir     fsize = GetFileSize( hFile, &fszExtra );
531*cdf0e10cSrcweir 
532*cdf0e10cSrcweir     // empty file, error or file to big
533*cdf0e10cSrcweir     if ( -1 == fsize || 0 == fsize || fszExtra )
534*cdf0e10cSrcweir         goto CLEANUP_AND_EXIT;
535*cdf0e10cSrcweir 
536*cdf0e10cSrcweir     hGlobal = GlobalAlloc( GMEM_MOVEABLE, fsize );
537*cdf0e10cSrcweir     if ( !hGlobal )
538*cdf0e10cSrcweir         goto CLEANUP_AND_EXIT;
539*cdf0e10cSrcweir 
540*cdf0e10cSrcweir     pData = GlobalLock( hGlobal );
541*cdf0e10cSrcweir     if ( !pData )
542*cdf0e10cSrcweir         goto CLEANUP_AND_EXIT;
543*cdf0e10cSrcweir 
544*cdf0e10cSrcweir     bRet = ReadFile(
545*cdf0e10cSrcweir         hFile, pData, fsize, &nBytesRead, NULL );
546*cdf0e10cSrcweir 
547*cdf0e10cSrcweir     if ( !bRet )
548*cdf0e10cSrcweir         goto CLEANUP_AND_EXIT;
549*cdf0e10cSrcweir 
550*cdf0e10cSrcweir     hr = CreateStreamOnHGlobal(
551*cdf0e10cSrcweir         hGlobal, sal_False, &pIStream );
552*cdf0e10cSrcweir 
553*cdf0e10cSrcweir     if ( SUCCEEDED( hr ) )
554*cdf0e10cSrcweir     {
555*cdf0e10cSrcweir         hr = OleLoadPicture(
556*cdf0e10cSrcweir             pIStream, fsize, sal_False,
557*cdf0e10cSrcweir             __uuidof( IPicture ), (LPVOID*)&m_IPicture );
558*cdf0e10cSrcweir     }
559*cdf0e10cSrcweir 
560*cdf0e10cSrcweir CLEANUP_AND_EXIT:
561*cdf0e10cSrcweir     if ( hFile )
562*cdf0e10cSrcweir         CloseHandle( hFile );
563*cdf0e10cSrcweir 
564*cdf0e10cSrcweir     if ( pData )
565*cdf0e10cSrcweir         GlobalUnlock( hGlobal );
566*cdf0e10cSrcweir 
567*cdf0e10cSrcweir     if ( hGlobal )
568*cdf0e10cSrcweir         GlobalFree( hGlobal );
569*cdf0e10cSrcweir 
570*cdf0e10cSrcweir     return ( SUCCEEDED( hr ) );
571*cdf0e10cSrcweir }
572*cdf0e10cSrcweir 
573*cdf0e10cSrcweir //---------------------------------------------------
574*cdf0e10cSrcweir //
575*cdf0e10cSrcweir //---------------------------------------------------
576*cdf0e10cSrcweir 
577*cdf0e10cSrcweir LRESULT CALLBACK CFilePreview::WndProc(
578*cdf0e10cSrcweir     HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
579*cdf0e10cSrcweir {
580*cdf0e10cSrcweir     LRESULT lResult = 0;
581*cdf0e10cSrcweir 
582*cdf0e10cSrcweir     switch( uMsg )
583*cdf0e10cSrcweir     {
584*cdf0e10cSrcweir     case WM_PAINT:
585*cdf0e10cSrcweir     {
586*cdf0e10cSrcweir         OSL_PRECOND( s_FilePreviewInst, "Static member not initialized" );
587*cdf0e10cSrcweir 
588*cdf0e10cSrcweir         HDC         hDC;
589*cdf0e10cSrcweir         PAINTSTRUCT ps;
590*cdf0e10cSrcweir 
591*cdf0e10cSrcweir         hDC = BeginPaint( hWnd, &ps );
592*cdf0e10cSrcweir         s_FilePreviewInst->onPaint( hWnd, hDC );
593*cdf0e10cSrcweir         EndPaint( hWnd, &ps );
594*cdf0e10cSrcweir     }
595*cdf0e10cSrcweir     break;
596*cdf0e10cSrcweir 
597*cdf0e10cSrcweir     // under windows 95/98 the creation of the
598*cdf0e10cSrcweir         // hidden target request window fails if
599*cdf0e10cSrcweir         // we don't handle this message ourself
600*cdf0e10cSrcweir         // because the DefWindowProc returns 0 as
601*cdf0e10cSrcweir         // a result of handling WM_NCCREATE what
602*cdf0e10cSrcweir         // leads to a failure of CreateWindow[Ex]!!!
603*cdf0e10cSrcweir     case WM_NCCREATE:
604*cdf0e10cSrcweir         lResult = sal_True;
605*cdf0e10cSrcweir         break;
606*cdf0e10cSrcweir 
607*cdf0e10cSrcweir     default:
608*cdf0e10cSrcweir         return DefWindowProc( hWnd, uMsg, wParam, lParam );
609*cdf0e10cSrcweir     }
610*cdf0e10cSrcweir 
611*cdf0e10cSrcweir     return lResult;
612*cdf0e10cSrcweir }
613*cdf0e10cSrcweir 
614*cdf0e10cSrcweir 
615*cdf0e10cSrcweir 
616