110ce8018SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
310ce8018SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
410ce8018SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
510ce8018SAndrew Rist  * distributed with this work for additional information
610ce8018SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
710ce8018SAndrew Rist  * to you under the Apache License, Version 2.0 (the
810ce8018SAndrew Rist  * "License"); you may not use this file except in compliance
910ce8018SAndrew Rist  * with the License.  You may obtain a copy of the License at
1010ce8018SAndrew Rist  *
1110ce8018SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1210ce8018SAndrew Rist  *
1310ce8018SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1410ce8018SAndrew Rist  * software distributed under the License is distributed on an
1510ce8018SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1610ce8018SAndrew Rist  * KIND, either express or implied.  See the License for the
1710ce8018SAndrew Rist  * specific language governing permissions and limitations
1810ce8018SAndrew Rist  * under the License.
1910ce8018SAndrew Rist  *
2010ce8018SAndrew Rist  *************************************************************/
2110ce8018SAndrew Rist 
2210ce8018SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _PREVIEWCTRL_HXX_
25cdf0e10cSrcweir #define _PREVIEWCTRL_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //------------------------------------------------------------------------
28cdf0e10cSrcweir // includes
29cdf0e10cSrcweir //------------------------------------------------------------------------
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <sal/types.h>
32cdf0e10cSrcweir #include <rtl/ustring.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <comdef.h>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <memory>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir //---------------------------------------------
39cdf0e10cSrcweir // declaration
40cdf0e10cSrcweir //---------------------------------------------
41cdf0e10cSrcweir 
42cdf0e10cSrcweir class CDimension
43cdf0e10cSrcweir {
44cdf0e10cSrcweir public:
CDimension()45cdf0e10cSrcweir     CDimension( ) :
46cdf0e10cSrcweir         m_cx( 0 ),
47cdf0e10cSrcweir         m_cy( 0 )
48cdf0e10cSrcweir 	{
49cdf0e10cSrcweir 	}
50cdf0e10cSrcweir 
CDimension(sal_Int32 cx,sal_Int32 cy)51cdf0e10cSrcweir     CDimension( sal_Int32 cx, sal_Int32 cy ) :
52cdf0e10cSrcweir         m_cx( cx ),
53cdf0e10cSrcweir         m_cy( cy )
54cdf0e10cSrcweir 	{
55cdf0e10cSrcweir 	}
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 	sal_Int32 m_cx;
58cdf0e10cSrcweir 	sal_Int32 m_cy;
59cdf0e10cSrcweir };
60cdf0e10cSrcweir 
61cdf0e10cSrcweir //--------------------------------------------------
62cdf0e10cSrcweir // we use OleInitialize here because we are calling
63cdf0e10cSrcweir // some Ole functions to realize the picture preview
64cdf0e10cSrcweir // and we expect to be called from the main thread
65cdf0e10cSrcweir // so that there will be no problem calling
66cdf0e10cSrcweir // OleInitialize (the main thread should be an STA)
67cdf0e10cSrcweir // When OleInitialize should fail at worst the
68cdf0e10cSrcweir // preview doesn't work
69cdf0e10cSrcweir //--------------------------------------------------
70cdf0e10cSrcweir 
71cdf0e10cSrcweir class CAutoOleInit
72cdf0e10cSrcweir {
73cdf0e10cSrcweir public:
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     // used to communicate ole
76cdf0e10cSrcweir     // initialzation failures
77cdf0e10cSrcweir     class COleInitException { };
78cdf0e10cSrcweir 
CAutoOleInit()79cdf0e10cSrcweir     CAutoOleInit( )
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         HRESULT hr = OleInitialize( NULL );
82cdf0e10cSrcweir         if ( FAILED( hr ) )
83cdf0e10cSrcweir             throw COleInitException( );
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir 
~CAutoOleInit()86cdf0e10cSrcweir     ~CAutoOleInit( )
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         OleUninitialize( );
89cdf0e10cSrcweir     }
90cdf0e10cSrcweir };
91cdf0e10cSrcweir 
92cdf0e10cSrcweir //---------------------------------------------
93cdf0e10cSrcweir // A simple file preview class to preview some
94cdf0e10cSrcweir // common picture formats like *.gif, *jpg, etc.
95cdf0e10cSrcweir // This class is not thread-safe and is
96cdf0e10cSrcweir // implmented as singleton, because the class
97cdf0e10cSrcweir // has only one static member to reconnect
98cdf0e10cSrcweir // from callback functions
99cdf0e10cSrcweir // we use a singleton-destroyer to get rid off
100cdf0e10cSrcweir // the singleton instance, but this happens
101cdf0e10cSrcweir // only on shutdown (unloading of the dll) -
102cdf0e10cSrcweir // it's a question of taste (other solutions
103cdf0e10cSrcweir // are possible)
104cdf0e10cSrcweir //---------------------------------------------
105cdf0e10cSrcweir 
106cdf0e10cSrcweir class CFilePreview
107cdf0e10cSrcweir {
108cdf0e10cSrcweir public:
109cdf0e10cSrcweir 	// to ensure only one instance (singleton)
110cdf0e10cSrcweir 	static CFilePreview* createInstance(
111cdf0e10cSrcweir 		HWND aParent,
112cdf0e10cSrcweir 		POINT ulCorner,
113cdf0e10cSrcweir 		const CDimension& aSize,
114cdf0e10cSrcweir 		HINSTANCE hInstance,
115cdf0e10cSrcweir 		sal_Bool bShow = sal_True,
116cdf0e10cSrcweir 		sal_Bool bEnabled = sal_True );
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 	// sets the size of the preview window
119cdf0e10cSrcweir 	sal_Bool SAL_CALL setSize( const CDimension& aSize );
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 	// returns the CDimension of the preview
122cdf0e10cSrcweir 	sal_Bool SAL_CALL getSize( CDimension& theSize ) const;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 	// sets the position of the upper left corner
125cdf0e10cSrcweir 	// of the preview window relative to the
126cdf0e10cSrcweir 	// upper left corner of the parent window
127cdf0e10cSrcweir 	sal_Bool SAL_CALL setPos( POINT ulCorner );
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 	// returns the current position of the preview
130cdf0e10cSrcweir 	// relative to the upper left corner of the
131cdf0e10cSrcweir 	// parent window
132cdf0e10cSrcweir 	sal_Bool SAL_CALL getPos( POINT& ulCorner ) const;
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 	// enables or disables the preview window
135cdf0e10cSrcweir 	// bEnable - true the window is enabled and updates its
136cdf0e10cSrcweir 	// view when update is called
137cdf0e10cSrcweir 	// bEnable - false the window shows itself in disabled
138cdf0e10cSrcweir 	// mode and does not update its view when update is
139cdf0e10cSrcweir 	// called
140cdf0e10cSrcweir 	void SAL_CALL enable( sal_Bool bEnable );
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 	// shows the preview window
143cdf0e10cSrcweir 	// possible values see SHOW_STATE
144cdf0e10cSrcweir 	sal_Bool SAL_CALL show( sal_Bool bShow );
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	// if the preview is shown and enabled
148cdf0e10cSrcweir 	// preview of the given file will be shown
149cdf0e10cSrcweir 	// returns true on success or false if an error
150*07a3d7f1SPedro Giffuni 	// occurred (the file in not there or not accessible etc.)
151cdf0e10cSrcweir 	virtual sal_Bool SAL_CALL update( const rtl::OUString& aFileName );
152cdf0e10cSrcweir 
153cdf0e10cSrcweir protected:
154cdf0e10cSrcweir 	// clients can create instances only through the static create method
155cdf0e10cSrcweir 	CFilePreview(
156cdf0e10cSrcweir 		HWND aParent,
157cdf0e10cSrcweir 		POINT ulCorner,
158cdf0e10cSrcweir 		const CDimension& aSize,
159cdf0e10cSrcweir 		HINSTANCE hInstance,
160cdf0e10cSrcweir 		sal_Bool bShow = sal_True,
161cdf0e10cSrcweir 		sal_Bool bEnabled = sal_True );
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 	// only the singleton destroyer class is allowed to delete the
164cdf0e10cSrcweir 	// singleton instance of this class
165cdf0e10cSrcweir 	virtual ~CFilePreview( );
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 	// we use the stl auto_ptr class as singleton destroyer
168cdf0e10cSrcweir 	typedef std::auto_ptr< CFilePreview > FILEPREVIEW_SINGLETON_DESTROYER_T;
169cdf0e10cSrcweir 
170cdf0e10cSrcweir protected:
171cdf0e10cSrcweir 	virtual void SAL_CALL onPaint( HWND hWnd, HDC hDC );
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 	sal_Bool loadFile( const rtl::OUString& aFileName );
174cdf0e10cSrcweir 
175cdf0e10cSrcweir private:
176cdf0e10cSrcweir     CAutoOleInit m_autoOleInit;
177cdf0e10cSrcweir 	POINT		 m_pt;
178cdf0e10cSrcweir 	CDimension	 m_dim;
179cdf0e10cSrcweir 	HWND		 m_hwnd;
180cdf0e10cSrcweir 	sal_Bool	 m_bEnabled;
181cdf0e10cSrcweir 	IPicturePtr  m_IPicture;
182cdf0e10cSrcweir 	ATOM		 m_atomPrevWndClass;
183cdf0e10cSrcweir 	HINSTANCE	 m_hInstance;
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 	static LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 	static CFilePreview* s_FilePreviewInst;
188cdf0e10cSrcweir 	static FILEPREVIEW_SINGLETON_DESTROYER_T s_SingletonDestroyer;
189cdf0e10cSrcweir 
190cdf0e10cSrcweir private:
191cdf0e10cSrcweir 	friend FILEPREVIEW_SINGLETON_DESTROYER_T;
192cdf0e10cSrcweir };
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 
195cdf0e10cSrcweir #endif
196