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