PageRenderTime 40ms CodeModel.GetById 27ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/3rd_party/wtl/AppWizCE/Files/Templates/1033/Frame.h

https://code.google.com/p/softart/
C Header | 341 lines | 267 code | 37 blank | 37 comment | 24 complexity | 312731ed2a768975307175ddf7783836 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, JSON, MPL-2.0-no-copyleft-exception, GPL-2.0, GPL-3.0, LGPL-3.0, BSD-2-Clause
  1// [!output WTL_FRAME_FILE].h : interface of the [!output WTL_FRAME_CLASS] class
  2//
  3/////////////////////////////////////////////////////////////////////////////
  4
  5#pragma once
  6
  7class [!output WTL_FRAME_CLASS] : public [!output WTL_FRAME_BASE_CLASS]<[!output WTL_FRAME_CLASS]>, public CUpdateUI<[!output WTL_FRAME_CLASS]>,
  8		public CMessageFilter, public CIdleHandler
  9{
 10public:
 11	DECLARE_FRAME_WND_CLASS(NULL, IDR_MAINFRAME)
 12
 13[!if WTL_APPTYPE_SDI || WTL_APPTYPE_MTSDI]
 14[!if WTL_USE_VIEW]
 15	[!output WTL_VIEW_CLASS] m_view;
 16
 17[!endif]
 18[!endif]
 19[!if WTL_USE_CPP_FILES]
 20	virtual BOOL PreTranslateMessage(MSG* pMsg);
 21[!else]
 22	virtual BOOL PreTranslateMessage(MSG* pMsg)
 23	{
 24[!if WTL_USE_VIEW]
 25		if([!output WTL_FRAME_BASE_CLASS]<[!output WTL_FRAME_CLASS]>::PreTranslateMessage(pMsg))
 26			return TRUE;
 27
 28		return m_view.PreTranslateMessage(pMsg);
 29[!else]
 30		return [!output WTL_FRAME_BASE_CLASS]<[!output WTL_FRAME_CLASS]>::PreTranslateMessage(pMsg);
 31[!endif]
 32	}
 33
 34[!endif]
 35[!if WTL_USE_CPP_FILES]
 36	virtual BOOL OnIdle();
 37[!else]
 38	virtual BOOL OnIdle()
 39	{
 40[!if WTL_USE_TOOLBAR]
 41		UIUpdateToolBar();
 42[!endif]
 43		return FALSE;
 44	}
 45[!endif]
 46
 47	BEGIN_UPDATE_UI_MAP([!output WTL_FRAME_CLASS])
 48[!if WTL_USE_TOOLBAR && !WTL_USE_AYGSHELL]
 49		UPDATE_ELEMENT(ID_VIEW_TOOLBAR, UPDUI_MENUPOPUP)
 50[!endif]
 51[!if WTL_USE_STATUSBAR]
 52		UPDATE_ELEMENT(ID_VIEW_STATUS_BAR, UPDUI_MENUPOPUP)
 53[!endif]
 54	END_UPDATE_UI_MAP()
 55
 56	BEGIN_MSG_MAP([!output WTL_FRAME_CLASS])
 57		MESSAGE_HANDLER(WM_CREATE, OnCreate)
 58[!if WTL_COM_SERVER]
 59		MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
 60[!endif]
 61[!if WTL_USE_SP03_COMPAT_MENUS]
 62		COMMAND_ID_HANDLER(ID_ACTION, OnAction)
 63[!endif]
 64		COMMAND_ID_HANDLER(ID_APP_EXIT, OnFileExit)
 65		COMMAND_ID_HANDLER(ID_FILE_NEW, OnFileNew)
 66[!if WTL_APPTYPE_MTSDI]
 67		COMMAND_ID_HANDLER(ID_FILE_NEW_WINDOW, OnFileNewWindow)
 68[!endif]
 69[!if WTL_USE_TOOLBAR && !WTL_USE_AYGSHELL]
 70		COMMAND_ID_HANDLER(ID_VIEW_TOOLBAR, OnViewToolBar)
 71[!endif]
 72[!if WTL_USE_STATUSBAR]
 73		COMMAND_ID_HANDLER(ID_VIEW_STATUS_BAR, OnViewStatusBar)
 74[!endif]
 75		COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
 76		CHAIN_MSG_MAP(CUpdateUI<[!output WTL_FRAME_CLASS]>)
 77		CHAIN_MSG_MAP([!output WTL_FRAME_BASE_CLASS]<[!output WTL_FRAME_CLASS]>)
 78	END_MSG_MAP()
 79
 80// Handler prototypes (uncomment arguments if needed):
 81//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
 82//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
 83//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)
 84
 85[!if WTL_USE_CPP_FILES]
 86	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
 87[!else]
 88	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
 89	{
 90[!if WTL_USE_AYGSHELL]
 91[!if !WTL_USE_SP03_COMPAT_MENUS]
 92		CreateSimpleCEMenuBar(IDR_MAINFRAME);
 93[!else]
 94		CreateSimpleCEMenuBar();
 95[!endif]
 96[!else]
 97		CreateSimpleCECommandBar(MAKEINTRESOURCE(IDR_MAINFRAME));
 98[!endif]
 99[!if WTL_USE_TOOLBAR]
100		CreateSimpleToolBar();
101[!endif]
102[!if WTL_USE_STATUSBAR]
103		CreateSimpleStatusBar();
104[!endif]
105[!if WTL_APPTYPE_SDI || WTL_APPTYPE_MTSDI]
106[!if WTL_USE_VIEW]
107[!if WTL_VIEWTYPE_FORM]
108
109		m_hWndClient = m_view.Create(m_hWnd);
110[!else]
111[!if WTL_VIEWTYPE_HTML]
112
113		//TODO: Replace with a URL of your choice
114		m_hWndClient = m_view.Create(m_hWnd, rcDefault, _T("http://www.microsoft.com"), [!output WTL_VIEW_STYLES]);
115[!else]
116
117		m_hWndClient = m_view.Create(m_hWnd, rcDefault, NULL, [!output WTL_VIEW_STYLES]);
118[!endif]
119[!endif]
120[!endif]
121[!endif]
122[!if WTL_USE_TOOLBAR]
123
124		UIAddToolBar(m_hWndToolBar);
125[!if !WTL_USE_AYGSHELL]
126		UISetCheck(ID_VIEW_TOOLBAR, 1);
127[!endif]
128[!endif]
129[!if WTL_USE_STATUSBAR]
130		UISetCheck(ID_VIEW_STATUS_BAR, 1);
131[!endif]
132
133		// register object for message filtering and idle updates
134		CMessageLoop* pLoop = _Module.GetMessageLoop();
135		ATLASSERT(pLoop != NULL);
136		pLoop->AddMessageFilter(this);
137		pLoop->AddIdleHandler(this);
138
139		return 0;
140	}
141
142[!endif]
143[!if WTL_COM_SERVER]
144[!if WTL_USE_CPP_FILES]
145	LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
146[!else]
147	LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
148	{
149		// unregister message filtering and idle updates
150		CMessageLoop* pLoop = _Module.GetMessageLoop();
151		ATLASSERT(pLoop != NULL);
152		pLoop->RemoveMessageFilter(this);
153		pLoop->RemoveIdleHandler(this);
154		// if UI is the last thread, no need to wait
155		if(_Module.GetLockCount() == 1)
156		{
157			_Module.m_dwTimeOut = 0L;
158			_Module.m_dwPause = 0L;
159		}
160		_Module.Unlock();
161[!if WTL_APPTYPE_MTSDI]
162		::PostQuitMessage(1);
163[!endif]
164		return 0;
165	}
166
167[!endif]
168[!endif]
169[!if WTL_USE_SP03_COMPAT_MENUS]
170[!if WTL_USE_CPP_FILES]
171	LRESULT OnAction(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
172[!else]
173	LRESULT OnAction(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
174	{
175		// TODO: add code
176
177		return 0;
178	}
179
180[!endif]
181[!endif]
182[!if WTL_USE_CPP_FILES]
183	LRESULT OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
184[!else]
185	LRESULT OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
186	{
187		PostMessage(WM_CLOSE);
188		return 0;
189	}
190
191[!endif]
192[!if WTL_USE_CPP_FILES]
193	LRESULT OnFileNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
194[!else]
195	LRESULT OnFileNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
196	{
197		// TODO: add code to initialize document
198
199		return 0;
200	}
201
202[!endif]
203[!if WTL_APPTYPE_MTSDI]
204[!if WTL_USE_CPP_FILES]
205	LRESULT OnFileNewWindow(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
206[!else]
207	LRESULT OnFileNewWindow(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
208	{
209		::PostThreadMessage(_Module.m_dwMainThreadID, WM_USER, 0, 0L);
210		return 0;
211	}
212
213[!endif]
214[!endif]
215[!if WTL_USE_TOOLBAR && !WTL_USE_AYGSHELL]
216[!if WTL_USE_CPP_FILES]
217	LRESULT OnViewToolBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
218[!else]
219	LRESULT OnViewToolBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
220	{
221		BOOL bVisible = !::IsWindowVisible(m_hWndToolBar);
222		::ShowWindow(m_hWndToolBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE);
223		UISetCheck(ID_VIEW_TOOLBAR, bVisible);
224		UpdateLayout();
225		return 0;
226	}
227
228[!endif]
229[!endif]
230[!if WTL_USE_STATUSBAR]
231[!if WTL_USE_CPP_FILES]
232	LRESULT OnViewStatusBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
233[!else]
234	LRESULT OnViewStatusBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
235	{
236		BOOL bVisible = !::IsWindowVisible(m_hWndStatusBar);
237		::ShowWindow(m_hWndStatusBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE);
238		UISetCheck(ID_VIEW_STATUS_BAR, bVisible);
239		UpdateLayout();
240		return 0;
241	}
242
243[!endif]
244[!endif]
245[!if WTL_USE_CPP_FILES]
246	LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
247[!else]
248	LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
249	{
250		CAboutDlg dlg;
251		dlg.DoModal();
252		return 0;
253	}
254[!endif]
255[!if WTL_USE_SINGLE_APP_INSTANCE]
256
257[!if WTL_USE_CPP_FILES]
258	static HRESULT ActivatePreviousInstance(HINSTANCE hInstance);
259[!else]
260	static HRESULT ActivatePreviousInstance(HINSTANCE hInstance)
261	{
262		CFrameWndClassInfo& classInfo = [!output WTL_FRAME_CLASS]::GetWndClassInfo();
263		ATLVERIFY(::LoadString(hInstance, IDR_MAINFRAME, classInfo.m_szAutoName, sizeof(classInfo.m_szAutoName)/sizeof(classInfo.m_szAutoName[0])));
264		classInfo.m_wc.lpszClassName = classInfo.m_szAutoName;
265		const TCHAR* pszClass = classInfo.m_wc.lpszClassName;
266		if(pszClass == NULL || *pszClass == _T('\0'))
267		{
268			return E_FAIL;
269		}
270
271		// Orginally 500ms in SmartPhone 2003 App Wizard generated code
272		// A lower value will result in a more responsive start-up of 
273		// the existing instance or termination of this instance.
274		const DWORD dRetryInterval = 100; 
275
276		// Orginally 5 in SmartPhone 2003 App Wizard generated code
277		// Multiplied by 5, since wait time was divided by 5.
278		const int iMaxRetries = 25;
279
280		for(int i = 0; i < iMaxRetries; ++i)
281		{
282			// Don't need ownership of the mutex
283			HANDLE hMutex = CreateMutex(NULL, FALSE, pszClass);
284
285			DWORD dw = GetLastError();
286
287			if(hMutex == NULL)
288			{
289				// ERROR_INVALID_HANDLE - A non-mutex object with this name already exists.
290				HRESULT hr = (dw == ERROR_INVALID_HANDLE) ? E_INVALIDARG : E_FAIL;
291				return hr;
292			}
293
294			// If the mutex already exists, then there should be another instance running
295			if(dw == ERROR_ALREADY_EXISTS)
296			{
297				// Just needed the error result, in this case, so close the handle.
298				CloseHandle(hMutex);
299
300				// Try to find the other instance, don't need to close HWND's.
301				// Don't check title in case it is changed by app after init.
302				HWND hwnd = FindWindow(pszClass, NULL);
303
304				if(hwnd == NULL)
305				{
306					// It's possible that the other istance is in the process of starting up or shutting down.
307					// So wait a bit and try again.
308					Sleep(dRetryInterval);
309					continue;
310				}
311				else
312				{
313					// Set the previous instance as the foreground window
314
315					// The "| 0x1" in the code below activates the correct owned window 
316					// of the previous instance's main window according to the SmartPhone 2003
317					// wizard generated code.
318					if(SetForegroundWindow(reinterpret_cast<HWND>(reinterpret_cast<ULONG>(hwnd) | 0x1)) != 0)
319					{
320						// S_FALSE indicates that another instance was activated, so this instance should terminate.
321						return S_FALSE;
322					}
323				}
324			}
325			else
326			{
327				// This is the first istance, so return S_OK.
328				// Don't close the mutext handle here.
329				// Do it on app shutdown instead.
330				return S_OK;
331			}
332		}
333
334		// The mutex was created by another instance, but it's window couldn't be brought
335		// to the foreground, so ssume  it's not a invalid instance (not this app, hung, etc.)
336		// and let this one start.
337		return S_OK;
338	}
339[!endif]
340[!endif]
341};