update for HEAD-2003091401
[reactos.git] / subsys / system / explorer / Seashell / SeaShellExt / UIApp.cpp
1 //*******************************************************************************
2 // COPYRIGHT NOTES
3 // ---------------
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.
10 // 
11 // For the latest updates to this code, check this site:
12 // http://www.masmex.com 
13 // after Sept 2000
14 // 
15 // Copyright(C) 2000 Philip Oldaker <email: philip@masmex.com>
16 //*******************************************************************************
17
18 #include "stdafx.h"
19 #include "UIFrameWnd.h"
20 #include "UIApp.h"
21
22 #ifdef _DEBUG
23 #define new DEBUG_NEW
24 #undef THIS_FILE
25 static char THIS_FILE[] = __FILE__;
26 #endif
27
28 /////////////////////////////////////////////////////////////////////////////
29 // CUIApp
30
31 IMPLEMENT_DYNAMIC(CUIApp,CWinApp)
32
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!
37         //}}AFX_MSG_MAP
38         // Standard file based document commands
39 END_MESSAGE_MAP()
40
41 /////////////////////////////////////////////////////////////////////////////
42 // CUIApp construction
43
44 CUIApp::CUIApp()  
45 {
46         // TODO: add construction code here,
47         // Place all significant initialization in InitInstance
48         m_IDMyIcon = 0;
49         m_bMyClassRegistered = false;
50 }
51
52 CDocTemplate *CUIApp::GetFirstDocTemplate()
53 {
54         POSITION posTempl = GetFirstDocTemplatePosition();
55         CDocTemplate *pTempl = GetNextDocTemplate(posTempl);
56         ASSERT(pTempl);
57         return pTempl;
58 }
59
60 CFrameWnd *CUIApp::GetMainFrame(CWnd *pWnd)
61 {
62         if (pWnd)
63                 return pWnd->GetParentFrame();
64         return (CFrameWnd*)m_pMainWnd;
65 }
66
67 CString CUIApp::GetRegAppKey()
68 {
69         CString sKey;
70         sKey += _T("Software");
71         sKey += _T("\\");
72         sKey += m_pszRegistryKey;
73         sKey += _T("\\");
74         sKey += m_pszProfileName;
75         sKey += _T("\\");
76         return sKey;
77 }
78
79 BOOL CUIApp::InitInstance()
80 {       
81         if (!m_sMyClassName.IsEmpty() && !RegisterMyClass())
82                 return FALSE;
83         return CWinApp::InitInstance();
84 }
85
86 int CUIApp::ExitInstance() 
87 {
88         // TODO: Add your specialized code here and/or call the base class
89     if(m_bMyClassRegistered)
90        ::UnregisterClass(m_sMyClassName,AfxGetInstanceHandle());
91
92         return CWinApp::ExitInstance();
93 }
94                                                                       
95 /////////////////////////////////////////////////////////////////////////////
96 // The one and only CUIApp object
97 bool CUIApp::RegisterMyClass()
98 {
99         // Register our unique class name so only one instance can be started
100         //////////////////////////////////////////////////////////////
101         ASSERT(!m_sMyClassName.IsEmpty());
102         if (m_sMyClassName.IsEmpty())
103                 return false;
104         //////////////////////////////////////////////////////////////
105     WNDCLASSEX wndcls; 
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();
111         if (m_IDMyIcon)
112         {
113                 wndcls.hIcon = LoadIcon(m_IDMyIcon); 
114                 wndcls.hIconSm = (HICON)LoadImage(AfxGetInstanceHandle(), 
115             MAKEINTRESOURCE(m_IDMyIcon),
116             IMAGE_ICON,
117             GetSystemMetrics(SM_CXSMICON),
118             GetSystemMetrics(SM_CYSMICON),
119             0);
120                         if (wndcls.hIconSm == NULL)
121                         {
122                                 ErrorMessage(_T("LoadImage failed"));
123                         }
124         }
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; 
130         WNDCLASSEX wndclsEx;
131         if (GetClassInfoEx(wndcls.hInstance, wndcls.lpszClassName, &wndclsEx))
132         {
133                 // class already registered
134                 return true;
135         }
136     // Register new class and exit if it fails
137     if(!::RegisterClassEx(&wndcls))
138     {
139                 ErrorMessage(_T("Class Registration Failed"));
140                 return false;
141     }
142         else
143                 TRACE(_T("Window class %s was registered in CUIApp\n"),GetMyClass());
144         m_bMyClassRegistered = true;
145         return true;
146 }
147
148 BOOL CUIApp::FirstInstance()
149 {
150         ASSERT(!m_sMyClassName.IsEmpty());
151         if (m_sMyClassName.IsEmpty())
152                 return TRUE;
153
154         CWnd *pWndPrev, *pWndChild;
155         // Determine if another window with our class name exists...
156         if (pWndPrev = CWnd::FindWindow(m_sMyClassName,NULL))
157         {
158                 // if so, does it have any popups?
159                 pWndChild = pWndPrev->GetLastActivePopup();
160
161                 // If iconic, restore the main window
162                 if (pWndPrev->IsIconic())
163                    pWndPrev->ShowWindow(SW_RESTORE);
164
165                 // Bring the main window or its popup to
166                 // the foreground
167                 pWndChild->SetForegroundWindow();
168
169                 // and we are done activating the previous one.
170                 return FALSE;
171         }
172         return TRUE;
173 }
174
175
176 /////////////////////////////////////////////////////////////////////////////
177 // CUIApp initialization
178 void CUIApp::SetStatusBarText(UINT nPaneID,LPCTSTR Text,UINT nIconID)
179 {
180         if (m_pMainWnd == NULL)
181         {
182                 TRACE1("Warning: '%s' SetStatusBarText called before main frame created!\n",Text);
183                 return;
184         }
185         CUIFrameWnd *pMainFrame = (CUIFrameWnd*)m_pMainWnd;
186         if (nIconID)
187                 pMainFrame->GetStatusBar().AddIcon(nPaneID,nIconID,false);
188         pMainFrame->GetStatusBar().SetText(nPaneID,Text,true);     
189 }
190
191 void CUIApp::SetStatusBarText(LPCTSTR Text)
192 {
193         SetStatusBarText(0,Text);
194 }
195
196 void CUIApp::SetStatusBarText(UINT nPaneID,LPCTSTR Text,CView *pView,UINT nIconID)
197 {
198         if (m_pMainWnd == NULL)
199                 return;
200         if (((CFrameWnd*)m_pMainWnd)->GetActiveView() == pView)
201                 SetStatusBarText(nPaneID,Text,nIconID);
202 }
203
204 void CUIApp::SetStatusBarText(UINT nPaneID,UINT nResID,UINT nIconID)
205 {
206         CString str;
207         str.LoadString(nResID);
208         SetStatusBarText(nPaneID,str,nIconID);
209 }
210
211 void CUIApp::SetStatusBarIcon(UINT nPaneID,UINT nIconID,bool bAdd)
212 {
213         if (m_pMainWnd == NULL)
214                 return;
215         CUIFrameWnd *pMainFrame = (CUIFrameWnd*)m_pMainWnd;
216         if (bAdd)
217                 pMainFrame->GetStatusBar().AddIcon(nPaneID,nIconID);
218         else
219                 pMainFrame->GetStatusBar().RemoveIcon(nPaneID,nIconID);
220         pMainFrame->GetStatusBar().RedrawWindow();
221 }
222
223 void CUIApp::SetStatusBarIdleMessage()
224 {
225         if (m_pMainWnd == NULL)
226                 return;
227         CUIFrameWnd *pMainFrame = (CUIFrameWnd*)m_pMainWnd;
228         pMainFrame->GetStatusBar().RemoveAllIcons(0);
229         SetStatusBarText(0,AFX_IDS_IDLEMESSAGE);
230 }
231
232 CDocument *CUIApp::GetDocument()
233 {
234         CDocTemplate *pTempl = GetFirstDocTemplate();
235         POSITION posDoc = pTempl->GetFirstDocPosition();
236         CDocument *pDoc = pTempl->GetNextDoc(posDoc);
237         ASSERT(pDoc);
238         return pDoc; 
239 }
240
241 CView *CUIApp::GetView(CRuntimeClass *pClass)
242 {
243         CDocTemplate *pTemplate = GetFirstDocTemplate();
244         POSITION posDoc = pTemplate->GetFirstDocPosition();
245         while (posDoc)
246         {
247                   CDocument *pDoc = pTemplate->GetNextDoc(posDoc); 
248                   POSITION posView = pDoc->GetFirstViewPosition();
249                   while (posView) 
250                   {
251                                 CView *pView = pDoc->GetNextView(posView); 
252                                 if (pView && pView->IsKindOf(pClass))
253                                         return pView;
254                   }
255     }
256         return NULL;
257 }
258
259 void CUIApp::ChangeProfile(LPCTSTR szRegistryKey,LPCTSTR szProfileName)
260 {
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);
267 }
268
269 void CUIApp::RestoreProfile()
270 {
271         if (!m_strOldProfileName.IsEmpty())
272         {
273                 free((void*)m_pszProfileName);
274                 m_pszProfileName = _tcsdup(m_strOldProfileName);
275         }
276         if (!m_strOldRegistryKey.IsEmpty()) 
277         {
278                 free((void*)m_pszRegistryKey);
279                 m_pszRegistryKey = _tcsdup(m_strOldRegistryKey);
280         }
281 }
282
283 /////////////////////////////////////////////////////////////////////////////
284 // CAppReg
285 // Tricks MFC into using another application key 
286 // useful if in HKEY_CURRENT_USER
287 CAppReg::CAppReg(CWinApp *pApp,LPCTSTR szRegistryKey,LPCTSTR szProfileName) 
288 {
289         ASSERT(pApp);
290         m_pApp = (CUIApp*)pApp;
291         m_pApp->ChangeProfile(szRegistryKey,szProfileName);
292 }
293
294 CAppReg::~CAppReg()
295 {
296         ASSERT(m_pApp);
297         m_pApp->RestoreProfile();
298 }
299
300 //static 
301 bool CUIApp::COMMessage(HRESULT hr,UINT nID)
302 {
303         CString sMess;
304         sMess.LoadString(nID);
305         return COMMessage(hr,sMess);
306 }
307
308 bool CUIApp::COMMessage(HRESULT hr,LPCTSTR szText)
309 {
310         if (FAILED(hr))
311         {
312                 ErrorMessage(szText,(DWORD)hr);
313                 return false;
314         }
315         return true;
316 }
317
318 void CUIApp::ErrorMessage(UINT nID,DWORD dwError)
319 {
320         CString sMess;
321         sMess.LoadString(nID);
322         ErrorMessage(sMess);
323 }
324
325 void CUIApp::ErrorMessage(LPCTSTR szText,DWORD dwError)
326 {
327         bool bError=true;
328         if (dwError == 0)
329         {
330                 dwError = GetLastError();
331                 bError = true;
332         }
333         else
334         {
335                 bError = FAILED(dwError);
336         }
337         LPVOID lpMsgBuf=NULL;
338         if (bError)
339         {
340                 FormatMessage( 
341                         FORMAT_MESSAGE_ALLOCATE_BUFFER | 
342                         FORMAT_MESSAGE_FROM_SYSTEM | 
343                         FORMAT_MESSAGE_IGNORE_INSERTS,
344                         NULL,
345                         dwError,
346                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
347                         (LPTSTR) &lpMsgBuf,
348                         0,
349                         NULL 
350                 );
351         }
352         // Process any inserts in lpMsgBuf.
353         // ...
354         // Display the string.
355         CString sMess;
356         if (lpMsgBuf)
357         {
358                 sMess = (LPCTSTR)lpMsgBuf;
359                 sMess += _T("\n");
360                 LocalFree( lpMsgBuf );  
361         }
362         if (szText)
363         {
364                 sMess += szText;
365         }
366         AfxMessageBox(sMess, MB_ICONSTOP );
367 }
368