1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _PREVIEWCTRL_HXX_
29 #define _PREVIEWCTRL_HXX_
30 
31 //------------------------------------------------------------------------
32 // includes
33 //------------------------------------------------------------------------
34 
35 #include <sal/types.h>
36 #include <rtl/ustring.hxx>
37 
38 #include <comdef.h>
39 
40 #include <memory>
41 
42 //---------------------------------------------
43 // declaration
44 //---------------------------------------------
45 
46 class CDimension
47 {
48 public:
49     CDimension( ) :
50         m_cx( 0 ),
51         m_cy( 0 )
52 	{
53 	}
54 
55     CDimension( sal_Int32 cx, sal_Int32 cy ) :
56         m_cx( cx ),
57         m_cy( cy )
58 	{
59 	}
60 
61 	sal_Int32 m_cx;
62 	sal_Int32 m_cy;
63 };
64 
65 //--------------------------------------------------
66 // we use OleInitialize here because we are calling
67 // some Ole functions to realize the picture preview
68 // and we expect to be called from the main thread
69 // so that there will be no problem calling
70 // OleInitialize (the main thread should be an STA)
71 // When OleInitialize should fail at worst the
72 // preview doesn't work
73 //--------------------------------------------------
74 
75 class CAutoOleInit
76 {
77 public:
78 
79     // used to communicate ole
80     // initialzation failures
81     class COleInitException { };
82 
83     CAutoOleInit( )
84     {
85         HRESULT hr = OleInitialize( NULL );
86         if ( FAILED( hr ) )
87             throw COleInitException( );
88     }
89 
90     ~CAutoOleInit( )
91     {
92         OleUninitialize( );
93     }
94 };
95 
96 //---------------------------------------------
97 // A simple file preview class to preview some
98 // common picture formats like *.gif, *jpg, etc.
99 // This class is not thread-safe and is
100 // implmented as singleton, because the class
101 // has only one static member to reconnect
102 // from callback functions
103 // we use a singleton-destroyer to get rid off
104 // the singleton instance, but this happens
105 // only on shutdown (unloading of the dll) -
106 // it's a question of taste (other solutions
107 // are possible)
108 //---------------------------------------------
109 
110 class CFilePreview
111 {
112 public:
113 	// to ensure only one instance (singleton)
114 	static CFilePreview* createInstance(
115 		HWND aParent,
116 		POINT ulCorner,
117 		const CDimension& aSize,
118 		HINSTANCE hInstance,
119 		sal_Bool bShow = sal_True,
120 		sal_Bool bEnabled = sal_True );
121 
122 	// sets the size of the preview window
123 	sal_Bool SAL_CALL setSize( const CDimension& aSize );
124 
125 	// returns the CDimension of the preview
126 	sal_Bool SAL_CALL getSize( CDimension& theSize ) const;
127 
128 	// sets the position of the upper left corner
129 	// of the preview window relative to the
130 	// upper left corner of the parent window
131 	sal_Bool SAL_CALL setPos( POINT ulCorner );
132 
133 	// returns the current position of the preview
134 	// relative to the upper left corner of the
135 	// parent window
136 	sal_Bool SAL_CALL getPos( POINT& ulCorner ) const;
137 
138 	// enables or disables the preview window
139 	// bEnable - true the window is enabled and updates its
140 	// view when update is called
141 	// bEnable - false the window shows itself in disabled
142 	// mode and does not update its view when update is
143 	// called
144 	void SAL_CALL enable( sal_Bool bEnable );
145 
146 	// shows the preview window
147 	// possible values see SHOW_STATE
148 	sal_Bool SAL_CALL show( sal_Bool bShow );
149 
150 
151 	// if the preview is shown and enabled
152 	// preview of the given file will be shown
153 	// returns true on success or false if an error
154 	// occured (the file in not there or not accessible etc.)
155 	virtual sal_Bool SAL_CALL update( const rtl::OUString& aFileName );
156 
157 protected:
158 	// clients can create instances only through the static create method
159 	CFilePreview(
160 		HWND aParent,
161 		POINT ulCorner,
162 		const CDimension& aSize,
163 		HINSTANCE hInstance,
164 		sal_Bool bShow = sal_True,
165 		sal_Bool bEnabled = sal_True );
166 
167 	// only the singleton destroyer class is allowed to delete the
168 	// singleton instance of this class
169 	virtual ~CFilePreview( );
170 
171 	// we use the stl auto_ptr class as singleton destroyer
172 	typedef std::auto_ptr< CFilePreview > FILEPREVIEW_SINGLETON_DESTROYER_T;
173 
174 protected:
175 	virtual void SAL_CALL onPaint( HWND hWnd, HDC hDC );
176 
177 	sal_Bool loadFile( const rtl::OUString& aFileName );
178 
179 private:
180     CAutoOleInit m_autoOleInit;
181 	POINT		 m_pt;
182 	CDimension	 m_dim;
183 	HWND		 m_hwnd;
184 	sal_Bool	 m_bEnabled;
185 	IPicturePtr  m_IPicture;
186 	ATOM		 m_atomPrevWndClass;
187 	HINSTANCE	 m_hInstance;
188 
189 	static LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
190 
191 	static CFilePreview* s_FilePreviewInst;
192 	static FILEPREVIEW_SINGLETON_DESTROYER_T s_SingletonDestroyer;
193 
194 private:
195 	friend FILEPREVIEW_SINGLETON_DESTROYER_T;
196 };
197 
198 
199 #endif
200