1 //*******************************************************************************
4 // You may use this source code, compile or redistribute it as part of your application
5 // for free. You cannot redistribute it as a part of a software development
6 // library without the agreement of the author. If the sources are
7 // distributed along with the application, you should leave the original
8 // copyright notes in the source code without any changes.
9 // This code can be used WITHOUT ANY WARRANTIES at your own risk.
11 // For the latest updates to this code, check this site:
12 // http://www.masmex.com
15 // Copyright(C) 2000 Philip Oldaker <email: philip@masmex.com>
16 //*******************************************************************************
19 #include "UIFrameWnd.h"
25 static char THIS_FILE[] = __FILE__;
28 /////////////////////////////////////////////////////////////////////////////
31 IMPLEMENT_DYNAMIC(CUIApp,CWinApp)
33 BEGIN_MESSAGE_MAP(CUIApp, CWinApp)
34 //{{AFX_MSG_MAP(CUIApp)
35 // NOTE - the ClassWizard will add and remove mapping macros here.
36 // DO NOT EDIT what you see in these blocks of generated code!
38 // Standard file based document commands
41 /////////////////////////////////////////////////////////////////////////////
42 // CUIApp construction
46 // TODO: add construction code here,
47 // Place all significant initialization in InitInstance
49 m_bMyClassRegistered = false;
52 CDocTemplate *CUIApp::GetFirstDocTemplate()
54 POSITION posTempl = GetFirstDocTemplatePosition();
55 CDocTemplate *pTempl = GetNextDocTemplate(posTempl);
60 CFrameWnd *CUIApp::GetMainFrame(CWnd *pWnd)
63 return pWnd->GetParentFrame();
64 return (CFrameWnd*)m_pMainWnd;
67 CString CUIApp::GetRegAppKey()
70 sKey += _T("Software");
72 sKey += m_pszRegistryKey;
74 sKey += m_pszProfileName;
79 BOOL CUIApp::InitInstance()
81 if (!m_sMyClassName.IsEmpty() && !RegisterMyClass())
83 return CWinApp::InitInstance();
86 int CUIApp::ExitInstance()
88 // TODO: Add your specialized code here and/or call the base class
89 if(m_bMyClassRegistered)
90 ::UnregisterClass(m_sMyClassName,AfxGetInstanceHandle());
92 return CWinApp::ExitInstance();
95 /////////////////////////////////////////////////////////////////////////////
96 // The one and only CUIApp object
97 bool CUIApp::RegisterMyClass()
99 // Register our unique class name so only one instance can be started
100 //////////////////////////////////////////////////////////////
101 ASSERT(!m_sMyClassName.IsEmpty());
102 if (m_sMyClassName.IsEmpty())
104 //////////////////////////////////////////////////////////////
106 memset(&wndcls, 0, sizeof(WNDCLASSEX));
107 wndcls.cbSize = sizeof(WNDCLASSEX);
108 wndcls.style = CS_DBLCLKS;// | CS_HREDRAW | CS_VREDRAW;
109 wndcls.lpfnWndProc = ::DefWindowProc;
110 wndcls.hInstance = AfxGetInstanceHandle();
113 wndcls.hIcon = LoadIcon(m_IDMyIcon);
114 wndcls.hIconSm = (HICON)LoadImage(AfxGetInstanceHandle(),
115 MAKEINTRESOURCE(m_IDMyIcon),
117 GetSystemMetrics(SM_CXSMICON),
118 GetSystemMetrics(SM_CYSMICON),
120 if (wndcls.hIconSm == NULL)
122 ErrorMessage(_T("LoadImage failed"));
125 wndcls.hCursor = LoadStandardCursor(IDC_ARROW);
126 wndcls.hbrBackground = NULL; //(HBRUSH) (COLOR_WINDOW + 1);
127 wndcls.lpszMenuName = NULL;
128 // Specify our own class name for using FindWindow later to set focus to running app
129 wndcls.lpszClassName = m_sMyClassName;
131 if (GetClassInfoEx(wndcls.hInstance, wndcls.lpszClassName, &wndclsEx))
133 // class already registered
136 // Register new class and exit if it fails
137 if(!::RegisterClassEx(&wndcls))
139 ErrorMessage(_T("Class Registration Failed"));
143 TRACE(_T("Window class %s was registered in CUIApp\n"),GetMyClass());
144 m_bMyClassRegistered = true;
148 BOOL CUIApp::FirstInstance()
150 ASSERT(!m_sMyClassName.IsEmpty());
151 if (m_sMyClassName.IsEmpty())
154 CWnd *pWndPrev, *pWndChild;
155 // Determine if another window with our class name exists...
156 if (pWndPrev = CWnd::FindWindow(m_sMyClassName,NULL))
158 // if so, does it have any popups?
159 pWndChild = pWndPrev->GetLastActivePopup();
161 // If iconic, restore the main window
162 if (pWndPrev->IsIconic())
163 pWndPrev->ShowWindow(SW_RESTORE);
165 // Bring the main window or its popup to
167 pWndChild->SetForegroundWindow();
169 // and we are done activating the previous one.
176 /////////////////////////////////////////////////////////////////////////////
177 // CUIApp initialization
178 void CUIApp::SetStatusBarText(UINT nPaneID,LPCTSTR Text,UINT nIconID)
180 if (m_pMainWnd == NULL)
182 TRACE1("Warning: '%s' SetStatusBarText called before main frame created!\n",Text);
185 CUIFrameWnd *pMainFrame = (CUIFrameWnd*)m_pMainWnd;
187 pMainFrame->GetStatusBar().AddIcon(nPaneID,nIconID,false);
188 pMainFrame->GetStatusBar().SetText(nPaneID,Text,true);
191 void CUIApp::SetStatusBarText(LPCTSTR Text)
193 SetStatusBarText(0,Text);
196 void CUIApp::SetStatusBarText(UINT nPaneID,LPCTSTR Text,CView *pView,UINT nIconID)
198 if (m_pMainWnd == NULL)
200 if (((CFrameWnd*)m_pMainWnd)->GetActiveView() == pView)
201 SetStatusBarText(nPaneID,Text,nIconID);
204 void CUIApp::SetStatusBarText(UINT nPaneID,UINT nResID,UINT nIconID)
207 str.LoadString(nResID);
208 SetStatusBarText(nPaneID,str,nIconID);
211 void CUIApp::SetStatusBarIcon(UINT nPaneID,UINT nIconID,bool bAdd)
213 if (m_pMainWnd == NULL)
215 CUIFrameWnd *pMainFrame = (CUIFrameWnd*)m_pMainWnd;
217 pMainFrame->GetStatusBar().AddIcon(nPaneID,nIconID);
219 pMainFrame->GetStatusBar().RemoveIcon(nPaneID,nIconID);
220 pMainFrame->GetStatusBar().RedrawWindow();
223 void CUIApp::SetStatusBarIdleMessage()
225 if (m_pMainWnd == NULL)
227 CUIFrameWnd *pMainFrame = (CUIFrameWnd*)m_pMainWnd;
228 pMainFrame->GetStatusBar().RemoveAllIcons(0);
229 SetStatusBarText(0,AFX_IDS_IDLEMESSAGE);
232 CDocument *CUIApp::GetDocument()
234 CDocTemplate *pTempl = GetFirstDocTemplate();
235 POSITION posDoc = pTempl->GetFirstDocPosition();
236 CDocument *pDoc = pTempl->GetNextDoc(posDoc);
241 CView *CUIApp::GetView(CRuntimeClass *pClass)
243 CDocTemplate *pTemplate = GetFirstDocTemplate();
244 POSITION posDoc = pTemplate->GetFirstDocPosition();
247 CDocument *pDoc = pTemplate->GetNextDoc(posDoc);
248 POSITION posView = pDoc->GetFirstViewPosition();
251 CView *pView = pDoc->GetNextView(posView);
252 if (pView && pView->IsKindOf(pClass))
259 void CUIApp::ChangeProfile(LPCTSTR szRegistryKey,LPCTSTR szProfileName)
261 m_strOldProfileName = m_pszProfileName;
262 m_strOldRegistryKey = m_pszRegistryKey;
263 free((void*)m_pszRegistryKey);
264 m_pszRegistryKey = _tcsdup(szRegistryKey);
265 free((void*)m_pszProfileName);
266 m_pszProfileName = _tcsdup(szProfileName);
269 void CUIApp::RestoreProfile()
271 if (!m_strOldProfileName.IsEmpty())
273 free((void*)m_pszProfileName);
274 m_pszProfileName = _tcsdup(m_strOldProfileName);
276 if (!m_strOldRegistryKey.IsEmpty())
278 free((void*)m_pszRegistryKey);
279 m_pszRegistryKey = _tcsdup(m_strOldRegistryKey);
283 /////////////////////////////////////////////////////////////////////////////
285 // Tricks MFC into using another application key
286 // useful if in HKEY_CURRENT_USER
287 CAppReg::CAppReg(CWinApp *pApp,LPCTSTR szRegistryKey,LPCTSTR szProfileName)
290 m_pApp = (CUIApp*)pApp;
291 m_pApp->ChangeProfile(szRegistryKey,szProfileName);
297 m_pApp->RestoreProfile();
301 bool CUIApp::COMMessage(HRESULT hr,UINT nID)
304 sMess.LoadString(nID);
305 return COMMessage(hr,sMess);
308 bool CUIApp::COMMessage(HRESULT hr,LPCTSTR szText)
312 ErrorMessage(szText,(DWORD)hr);
318 void CUIApp::ErrorMessage(UINT nID,DWORD dwError)
321 sMess.LoadString(nID);
325 void CUIApp::ErrorMessage(LPCTSTR szText,DWORD dwError)
330 dwError = GetLastError();
335 bError = FAILED(dwError);
337 LPVOID lpMsgBuf=NULL;
341 FORMAT_MESSAGE_ALLOCATE_BUFFER |
342 FORMAT_MESSAGE_FROM_SYSTEM |
343 FORMAT_MESSAGE_IGNORE_INSERTS,
346 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
352 // Process any inserts in lpMsgBuf.
354 // Display the string.
358 sMess = (LPCTSTR)lpMsgBuf;
360 LocalFree( lpMsgBuf );
366 AfxMessageBox(sMess, MB_ICONSTOP );