]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - tools/src/tools/configtool/standalone/win32/Configtool.cpp
Initial revision
[karo-tx-redboot.git] / tools / src / tools / configtool / standalone / win32 / Configtool.cpp
1 //####COPYRIGHTBEGIN####
2 //                                                                          
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
5 //
6 // This program is part of the eCos host tools.
7 //
8 // This program is free software; you can redistribute it and/or modify it 
9 // under the terms of the GNU General Public License as published by the Free 
10 // Software Foundation; either version 2 of the License, or (at your option) 
11 // any later version.
12 // 
13 // This program is distributed in the hope that it will be useful, but WITHOUT 
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
15 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
16 // more details.
17 // 
18 // You should have received a copy of the GNU General Public License along with
19 // this program; if not, write to the Free Software Foundation, Inc., 
20 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 //
22 // ----------------------------------------------------------------------------
23 //                                                                          
24 //####COPYRIGHTEND####
25 // ConfigTool.cpp : Defines the class behaviors for the application.
26 //
27 //
28 //===========================================================================
29 //#####DESCRIPTIONBEGIN####
30 //
31 // Author(s):   sdf
32 // Contact(s):  sdf
33 // Date:                1998/08/11
34 // Version:             0.01
35 // Purpose:     
36 // Description: This is the implementation of the application class
37 // Requires:    
38 // Provides:    
39 // See also:    
40 // Known bugs:  
41 // Usage:       
42 //
43 //####DESCRIPTIONEND####
44 //
45 //===========================================================================
46
47 #include "stdafx.h"
48
49 #include "ConfigTool.h"
50 #include "ConfigToolDoc.h"
51 #include "ControlView.h"
52 #include "CSHDialog.h"
53 #include "CTUtils.h"
54 #include "eCosDialog.h"
55 #include "eCosTest.h"
56 #include "eCosSocket.h"
57 #include "FileName.h"
58 #include "MainFrm.h"
59 #include "OutputView.h"
60 #include "RegKeyEx.h"
61 #include "Splash.h"
62
63 #include <afxadv.h>
64 #include <afxdisp.h> // for AfxEnableControlContainer()
65 #ifdef _DEBUG
66 #define new DEBUG_NEW
67 #undef THIS_FILE
68 static char THIS_FILE[] = __FILE__;
69 #endif
70
71 CString CConfigTool::strHelpFile;
72
73 /////////////////////////////////////////////////////////////////////////////
74 // CConfigToolApp 
75
76 BEGIN_MESSAGE_MAP(CConfigToolApp, CWinApp)
77 //{{AFX_MSG_MAP(CConfigToolApp)
78 ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
79 //}}AFX_MSG_MAP
80 // Standard file based document commands
81 ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
82 ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
83 // Standard print setup command
84 ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
85 END_MESSAGE_MAP()
86
87
88
89 /////////////////////////////////////////////////////////////////////////////
90 // CConfigToolApp construction
91 CConfigToolApp::CConfigToolApp()
92 {
93   m_GrayPen.CreatePen(PS_SOLID,1,RGB(192,192,192));     
94   m_VersionInfo.dwOSVersionInfoSize=sizeof OSVERSIONINFO;
95   ::GetVersionEx(&m_VersionInfo);
96 }
97
98 /////////////////////////////////////////////////////////////////////////////
99 // The one and only CConfigToolApp object
100
101 CConfigToolApp theApp;
102
103 /////////////////////////////////////////////////////////////////////////////
104 // CConfigToolApp initialization
105
106 BOOL CConfigToolApp::InitInstance()
107 {
108   CeCosSocket::Init();
109   CeCosTestPlatform::Load();
110   CFileName strCSHFile;
111   ::GetModuleFileName(::GetModuleHandle(NULL),strCSHFile.GetBuffer(1+MAX_PATH),MAX_PATH);
112   strCSHFile.ReleaseBuffer();
113   strCSHFile.ReplaceExtension(_T(".chm"));
114   CConfigTool::strHelpFile=strCSHFile;
115   CCSHDialog::SetCSHFilePath(strCSHFile);
116   
117   extern UINT  arCommonDialogMap[];
118   CeCosDialog::AddDialogMap(arCommonDialogMap);
119
120   extern UINT  arStandaloneDialogMap[];
121   CeCosDialog::AddDialogMap(arStandaloneDialogMap);
122
123   extern UINT  arPkgAdminDialogMap[];
124   CeCosDialog::AddDialogMap(arPkgAdminDialogMap);
125
126   extern UINT  arTestToolDialogMap[];
127   CeCosDialog::AddDialogMap(arTestToolDialogMap);
128
129   // CG: The following block was added by the Splash Screen component.
130   \
131   {
132     \
133       CCommandLineInfo cmdInfo;
134     \
135       ParseCommandLine(cmdInfo);
136     \
137       
138       \
139       CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);
140     \
141   }
142   AfxEnableControlContainer();
143   // Standard initialization
144   // If you are not using these features and wish to reduce the size
145   //  of your final executable, you should remove from the following
146   //  the specific initialization routines you do not need.
147   
148         int nSize=GetEnvironmentVariable(_T("PATH"), NULL, 0);
149         if(nSize>0){
150     GetEnvironmentVariable(_T("PATH"),m_strOriginalPath.GetBuffer(1+nSize),nSize);
151     m_strOriginalPath.ReleaseBuffer();
152   }
153
154 #ifdef _AFXDLL
155   Enable3dControls();                   // Call this when using MFC in a shared DLL
156 #else
157   Enable3dControlsStatic();     // Call this when linking to MFC statically
158 #endif
159   SetRegistryKey(IDS_REGKEY);
160   LoadStdProfileSettings();  // Load standard INI file options (including MRU)
161   
162   // Register the application's document templates.  Document templates
163   //  serve as the connection between documents, frame windows and views.
164   
165   CSingleDocTemplate* pDocTemplate;
166   pDocTemplate = new CSingleDocTemplate(
167     IDR_MAINFRAME,
168     RUNTIME_CLASS(CConfigToolDoc),
169     RUNTIME_CLASS(CMainFrame),       // main SDI frame window
170     RUNTIME_CLASS(CControlView));
171   AddDocTemplate(pDocTemplate);
172   
173   // Parse command line for standard shell commands, DDE, file open
174   CCommandLineInfo cmdInfo;
175   //ParseCommandLine(cmdInfo);
176   
177   CString strCmdLine(GetCommandLine());
178   CStringArray arArgs;
179   int nWords=CUtils::Chop(strCmdLine,arArgs,_TCHAR(' '),/*bObserveStrings=*/true,/*bBackslashQuotes=*/false);
180   bool bBuild=false;
181   bool bRun=false;
182   CConfigToolDoc *pDoc=CConfigTool::GetConfigToolDoc();
183   for(int i=1;i<nWords;i++)
184   {
185     CString &str=arArgs[i];
186     if(0==str.Compare(_T("-R")) && i<nWords-1){
187       pDoc->SetRepository(arArgs[++i]);
188     } else if (0==str.CompareNoCase(_T("-S")) && i<nWords-1){
189       // Load settings file
190     } else if (0==str.CompareNoCase(_T("-B"))){
191       // Build
192       bBuild=true;
193     } else if (0==str.CompareNoCase(_T("-R"))){
194       // run
195       bRun=true;
196     } else {
197       cmdInfo.m_nShellCommand=CCommandLineInfo::FileOpen;
198       cmdInfo.m_strFileName=str;
199     }
200   }
201   
202   // Dispatch commands specified on the command line
203   if (!ProcessShellCommand(cmdInfo))
204     return FALSE;
205   
206   CMenu* pMenu = m_pMainWnd->GetMenu();
207   if (pMenu)pMenu->DestroyMenu();
208   HMENU hMenu = ((CMainFrame*) m_pMainWnd)->NewMenu();
209   pMenu = CMenu::FromHandle( hMenu );
210   m_pMainWnd->SetMenu(pMenu);
211   ((CMainFrame*)m_pMainWnd)->m_hMenuDefault = hMenu;
212
213
214   // The one and only window has been initialized, so show and update it.
215   m_pMainWnd->ShowWindow(SW_SHOW);
216   m_pMainWnd->UpdateWindow();
217   
218   return TRUE;
219 }
220
221 /////////////////////////////////////////////////////////////////////////////
222 // CAboutDlg dialog used for App About
223
224 class CAboutDlg : public CDialog
225 {
226 public:
227   CAboutDlg();
228   
229   // Dialog Data
230   //{{AFX_DATA(CAboutDlg)
231   enum { IDD = IDD_ABOUTBOX };
232   CStatic       m_static;
233   //}}AFX_DATA
234   
235   // ClassWizard generated virtual function overrides
236   //{{AFX_VIRTUAL(CAboutDlg)
237 protected:
238   virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
239   //}}AFX_VIRTUAL
240   
241   // Implementation
242 protected:
243   //{{AFX_MSG(CAboutDlg)
244   virtual BOOL OnInitDialog();
245   afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
246   //}}AFX_MSG
247   DECLARE_MESSAGE_MAP()
248 };
249
250 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
251 {
252   //{{AFX_DATA_INIT(CAboutDlg)
253   //}}AFX_DATA_INIT
254 }
255
256 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
257 {
258   CDialog::DoDataExchange(pDX);
259   //{{AFX_DATA_MAP(CAboutDlg)
260   DDX_Control(pDX, IDC_STATIC_ABOUT, m_static);
261   //}}AFX_DATA_MAP
262 }
263
264 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
265 //{{AFX_MSG_MAP(CAboutDlg)
266 ON_WM_LBUTTONDBLCLK()
267 //}}AFX_MSG_MAP
268 END_MESSAGE_MAP()
269
270 // App command to run the dialog
271 void CConfigToolApp::OnAppAbout()
272 {
273   CAboutDlg aboutDlg;
274   aboutDlg.DoModal();
275 }
276
277 /////////////////////////////////////////////////////////////////////////////
278 // CConfigToolApp commands
279
280
281 int CConfigToolApp::ExitInstance() 
282 {
283   // Save persistence info
284   WriteProfileString(_T("Build"),        _T("MakeOptions"),m_strMakeOptions);   
285   WriteProfileString(CUtils::LoadString(IDS_KEY_USER_DIR), _T("Folder"), m_strUserToolsDir);
286   // Write any target bindirs to the registry
287   for(POSITION pos = m_arstrBinDirs.GetStartPosition(); pos != NULL; ){
288     CString strPrefix,strBinDir;
289     m_arstrBinDirs.GetNextAssoc(pos, strPrefix, strBinDir);
290     WriteProfileString(CUtils::LoadString(IDS_KEY_TOOLS_DIR),strPrefix,strBinDir);
291   }
292   ::DeleteFile(CConfigToolDoc::HTMLHelpLinkFileName());
293   CeCosSocket::Term();
294   CeCosTestPlatform::RemoveAllPlatforms();
295   return CWinApp::ExitInstance();
296 }
297
298 void CConfigToolApp::LoadStdProfileSettings()
299 {
300   CWinApp::LoadStdProfileSettings(4);
301   
302   CString strNotepad;
303   FindExecutable(_T("Notepad.exe"),_T(""),strNotepad.GetBuffer(MAX_PATH));
304   strNotepad.ReleaseBuffer(); 
305   
306   m_strMakeOptions=                       GetProfileString(_T("Build"), _T("MakeOptions"),_T(""));
307   if(m_strMakeOptions.IsEmpty()){
308     SYSTEM_INFO SystemInfo;
309     GetSystemInfo(&SystemInfo);
310     m_strMakeOptions.Format(_T("-j%d"),SystemInfo.dwNumberOfProcessors);
311   }
312
313   CRegKeyEx InstallKey (HKEY_LOCAL_MACHINE, GetInstallVersionKey (), KEY_READ);
314
315   m_strUserToolsDir=                    GetProfileString(CUtils::LoadString(IDS_KEY_USER_DIR), _T("Folder"), _T(""));
316   if(!m_strUserToolsDir.IsDir()){ // if the user specified user tools dir does not exist
317     InstallKey.QueryValue (_T("Default User Tools Path"), m_strUserToolsDir); // use the installer default
318     if (!m_strUserToolsDir.IsDir()) { // if the default user tools dir does not exist
319       m_strUserToolsDir=_T(""); // force prompting for the user tools dir
320     }
321   }
322   
323   // set default build tools binary directories as specified by the installer
324   CFileName strDefaultBuildToolsPath;
325   if (InstallKey.QueryValue (_T("Default Build Tools Path"), strDefaultBuildToolsPath)) {
326     // look for *-gcc.exe in the default build tools directory
327     CFileFind finder;
328     BOOL bMore=finder.FindFile (strDefaultBuildToolsPath + _T("*-gcc.exe"));
329     while (bMore) { // for each file matching the globbing pattern
330       bMore = finder.FindNextFile ();
331       CFileName strFile (finder.GetFileName ());
332       m_arstrBinDirs.SetAt (strFile.Left (strFile.Find (_T("-gcc"))), strDefaultBuildToolsPath);
333     }
334   }
335
336   CRegKeyEx k;
337   k.Attach(GetSectionKey(CUtils::LoadString(IDS_KEY_TOOLS_DIR)));
338   CFileName strDir;
339   CString strPrefix;
340   for(int i=0;k.QueryValue(i,strPrefix,strDir);i++){
341     if(strDir.IsDir()){
342       m_arstrBinDirs.SetAt(strPrefix,strDir);
343     }
344   }
345
346   CStringArray arstrToolChainPaths;
347   GetRepositoryRegistryClues(arstrToolChainPaths,_T("GNUPro eCos"));
348   for(i=0;i<arstrToolChainPaths.GetSize();i++){
349     CFileName strDir(arstrToolChainPaths[i]);
350     strDir+="H-i686-cygwin32\\bin";
351     if(strDir.IsDir()){
352       // This is a potential toolchain location. Look for *-gcc.exe
353       CFileFind finder;
354       BOOL bMore=finder.FindFile(strDir+"*-gcc.exe");
355       while (bMore) {
356         bMore = finder.FindNextFile();
357         CFileName strFile(finder.GetFileName());
358         m_arstrBinDirs.SetAt(strFile.Left(strFile.Find(_T("-gcc"))),strDir);
359       }
360     }
361   }
362
363   // Look for GNUPro 00r1 first, since it's the latest and greatest user tools.   
364   GetRepositoryRegistryClues(m_arstrUserToolPaths, _T("GNUPro 00r1"));
365   if (m_arstrUserToolPaths.GetSize() == 0)
366   {
367       GetRepositoryRegistryClues(m_arstrUserToolPaths, _T("Cygwin 00r1"));
368   }
369   if (m_arstrUserToolPaths.GetSize() > 0)
370   {
371     for(i=0;i<m_arstrUserToolPaths.GetSize();i++){
372         CFileName str(m_arstrUserToolPaths[i]);
373         str+="H-i686-pc-cygwin\\bin";
374         if(str.IsDir()){
375         m_arstrUserToolPaths.SetAt(i,str);
376         } else {
377         m_arstrUserToolPaths.RemoveAt(i);
378         i--;
379         }
380     }
381   }
382   else
383   {   
384     GetRepositoryRegistryClues(m_arstrUserToolPaths, _T("GNUPro unsupported"));
385     for(i=0;i<m_arstrUserToolPaths.GetSize();i++){
386         CFileName str(m_arstrUserToolPaths[i]);
387         str+="H-i686-cygwin32\\bin";
388         if(str.IsDir()){
389         m_arstrUserToolPaths.SetAt(i,str);
390         } else {
391         m_arstrUserToolPaths.RemoveAt(i);
392         i--;
393         }
394     }
395  }
396   
397   // Include the path in the set of potential user paths
398   {
399     CString strPath;
400     int nSize=GetEnvironmentVariable(CUtils::LoadString(IDS_PATH), NULL, 0);
401     if(nSize>0){
402       GetEnvironmentVariable(CUtils::LoadString(IDS_PATH), strPath.GetBuffer(nSize), nSize);
403       strPath.ReleaseBuffer();
404       CStringArray arstrPath;
405       CUtils::Chop(strPath,arstrPath,_TCHAR(';'));
406       for(int i=arstrPath.GetSize()-1;i>=0;--i){ // Reverse order is important to treat path correctly
407         const CFileName &strFolder=arstrPath[i];
408         CFileName strFile(strFolder);
409         strFile+=_T("ls.exe");
410         if(strFile.Exists()){
411           m_arstrUserToolPaths.Add(strFolder);
412           if(m_strUserToolsDir.IsEmpty()){
413             m_strUserToolsDir=strFolder;
414           }
415         }
416       }
417     }
418   }
419 }
420
421 BOOL CConfigToolApp::OnIdle(LONG lCount) 
422 {
423   if(lCount==0)
424   {
425     CMainFrame *pMain=CConfigTool::GetMain();
426     if(pMain){
427       // During startup the main window will be the splash screen
428       CDC *pDC=pMain->GetDC();
429       CFont *pOldFont=pDC->SelectObject(pMain->m_wndStatusBar.GetFont());
430       pDC->SelectObject(pOldFont);
431       pMain->ReleaseDC(pDC);
432       
433     }
434   }
435   return CWinApp::OnIdle(lCount);
436 }
437
438 BOOL CConfigToolApp::PreTranslateMessage(MSG* pMsg)
439 {
440   // CG: The following lines were added by the Splash Screen component.
441   if (CSplashWnd::PreTranslateAppMessage(pMsg))
442     return TRUE;
443   
444   return CWinApp::PreTranslateMessage(pMsg);
445 }
446
447 BOOL CAboutDlg::OnInitDialog() 
448 {
449   CString strVersion;
450   strVersion.Format(_T("%s %s"),_T(__DATE__),_T(__TIME__));
451   SetDlgItemText(IDC_STATIC_DATETIME,strVersion);
452   CDialog::OnInitDialog();
453   return TRUE;  // return TRUE unless you set the focus to a control
454   // EXCEPTION: OCX Property Pages should return FALSE
455 }
456
457 bool CConfigToolApp::Launch(const CString & strFileName,const CString &strViewer)
458 {
459   bool rc=false;
460   
461   if(!strViewer.IsEmpty())//use custom editor
462   {
463     CString strCmdline(strViewer);
464     
465     TCHAR *pszCmdLine=strCmdline.GetBuffer(strCmdline.GetLength());
466     GetShortPathName(pszCmdLine,pszCmdLine,strCmdline.GetLength());
467     strCmdline.ReleaseBuffer();
468     
469     strCmdline+=_TCHAR(' ');
470     strCmdline+=strFileName;
471     PROCESS_INFORMATION pi;
472     STARTUPINFO si;
473     
474     si.cb = sizeof(STARTUPINFO); 
475     si.lpReserved = NULL; 
476     si.lpReserved2 = NULL; 
477     si.cbReserved2 = 0; 
478     si.lpDesktop = NULL; 
479     si.dwFlags = 0; 
480     si.lpTitle=NULL;
481     
482     if(CreateProcess(
483       NULL, // app name
484       //strCmdline.GetBuffer(strCmdline.GetLength()),    // command line
485       strCmdline.GetBuffer(strCmdline.GetLength()),    // command line
486       NULL, // process security
487       NULL, // thread security
488       TRUE, // inherit handles
489       0,
490       NULL, // environment
491       NULL, // current dir
492       &si, // startup info
493       &pi)){
494       CloseHandle(pi.hProcess);
495       CloseHandle(pi.hThread);
496       rc=true;
497     } else {
498       CUtils::MessageBoxF(_T("Failed to invoke %s.\n"),strCmdline);
499     }
500     strCmdline.ReleaseBuffer();
501   } else {// Use association
502     TCHAR szExe[MAX_PATH];
503     HINSTANCE h=FindExecutable(strFileName,_T("."),szExe);
504     if(int(h)<=32){
505       CString str;
506       switch(int(h)){
507       case 0:  str=_T("The system is out of memory or resources.");break;
508       case 31: str=_T("There is no association for the specified file type.");break;
509       case ERROR_FILE_NOT_FOUND: str=_T("The specified file was not found.");break;
510       case ERROR_PATH_NOT_FOUND: str=_T("The specified path was not found.");break;
511       case ERROR_BAD_FORMAT:     str=_T("The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).");break;
512       default: break;
513       }
514       CUtils::MessageBoxF(_T("Failed to open document %s.\r\n%s"),strFileName,str);
515     } else {
516       
517       SHELLEXECUTEINFO sei = {sizeof(sei), 0, AfxGetMainWnd()->GetSafeHwnd(), _T("open"),
518         strFileName, NULL, NULL, SW_SHOWNORMAL, AfxGetInstanceHandle( )};
519       
520       sei.hInstApp=0;
521       HINSTANCE hInst=ShellExecute(AfxGetMainWnd()->GetSafeHwnd(),_T("open"), strFileName, NULL, _T("."), 0)/*ShellExecuteEx(&sei)*/;
522       if(int(hInst)<=32/*sei.hInstApp==0*/)
523       {
524         CString str;
525         switch(int(hInst))
526         {
527         case 0 : str=_T("The operating system is out of memory or resources. ");break;
528         case ERROR_FILE_NOT_FOUND : str=_T("The specified file was not found. ");break;
529         case ERROR_PATH_NOT_FOUND : str=_T("The specified path was not found. ");break;
530         case ERROR_BAD_FORMAT : str=_T("The .EXE file is invalid (non-Win32 .EXE or error in .EXE image). ");break;
531         case SE_ERR_ACCESSDENIED : str=_T("The operating system denied access to the specified file. ");break;
532         case SE_ERR_ASSOCINCOMPLETE : str=_T("The filename association is incomplete or invalid. ");break;
533         case SE_ERR_DDEBUSY : str=_T("The DDE transaction could not be completed because other DDE transactions were being processed. ");break;
534         case SE_ERR_DDEFAIL : str=_T("The DDE transaction failed. ");break;
535         case SE_ERR_DDETIMEOUT : str=_T("The DDE transaction could not be completed because the request timed out. ");break;
536         case SE_ERR_DLLNOTFOUND : str=_T("The specified dynamic-link library was not found. ");break;
537           //case SE_ERR_FNF : str=_T("The specified file was not found. ");break;
538         case SE_ERR_NOASSOC : str=_T("There is no application associated with the given filename extension. ");break;
539         case SE_ERR_OOM : str=_T("There was not enough memory to complete the operation. ");break;
540           //case SE_ERR_PNF : str=_T("The specified path was not found. ");break;
541         case SE_ERR_SHARE : str=_T("A sharing violation occurred. ");break;
542         default: str=_T("An unexpected error occurred");break;
543         }
544         CUtils::MessageBoxF(_T("Failed to open document %s using %s.\r\n%s"),strFileName,szExe,str);
545       } else {
546         rc=true;
547       }
548     }
549   }
550   return rc;
551 }
552
553 /*
554 void CConfigToolApp::AddToRecentFileList(LPCTSTR lpszPathName) 
555 {
556 const CFileName strDir=CFileName(lpszPathName).Head();
557 if(strDir.IsDir()){
558 CWinApp::AddToRecentFileList(strDir);
559 }
560 }
561 */
562
563 int CConfigToolApp::GetRepositoryRegistryClues(CStringArray &arstr,LPCTSTR pszPrefix)
564 {
565   // Go looking for potential candidates in SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
566   arstr.RemoveAll();    
567   CRegKeyEx k1(HKEY_LOCAL_MACHINE,_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths"),KEY_READ);
568   CString strKey;
569   for(int i=0;k1.QueryKey(i,strKey);i++){
570     if(0==strKey.Find(pszPrefix)){
571       CRegKeyEx k2((HKEY)k1,strKey,KEY_READ);
572       CFileName strDir;
573       if(k2.QueryValue(_T("Path"),strDir) && strDir.IsDir()){
574         arstr.Add(strDir);
575       }
576     }
577   }   
578   return arstr.GetSize();
579 }
580
581
582 void CAboutDlg::OnLButtonDblClk(UINT nFlags, CPoint point) 
583 {
584   UNUSED_ALWAYS(point);
585   UNUSED_ALWAYS(nFlags);
586 }
587
588 //const char CConfigToolApp::s_profileFlags[] = _T("flags");
589 LPCTSTR  CConfigToolApp::s_profileMax = _T("max");
590 LPCTSTR  CConfigToolApp::s_profileTool = _T("tool");
591 LPCTSTR  CConfigToolApp::s_profileStatus = _T("status");
592 LPCTSTR  CConfigToolApp::s_profileRect = _T("Rect");
593
594 void CConfigToolApp::SaveWindowPlacement(CWnd *pWnd, const CString &strKey)
595 {
596   // Our position
597   CString strText;
598   
599   WINDOWPLACEMENT wndpl;
600   wndpl.length = sizeof(WINDOWPLACEMENT);
601   // gets current window position and
602   //  iconized/maximized status
603   pWnd->GetWindowPlacement(&wndpl);
604   strText.Format(_T("%04d %04d %04d %04d"),
605     wndpl.rcNormalPosition.left,
606     wndpl.rcNormalPosition.top,
607     wndpl.rcNormalPosition.right,
608     wndpl.rcNormalPosition.bottom);
609   if(!pWnd->IsWindowVisible()){
610     wndpl.showCmd=SW_HIDE;
611   }
612   WriteProfileInt   (strKey,_T("Show"), wndpl.showCmd);
613   WriteProfileString(strKey,s_profileRect, strText);
614 }
615
616 BOOL CConfigToolApp::RestoreWindowPlacement(CWnd *pWnd,const CString &strKey,const CRect &rcDefault)
617 {
618   // Set the windows according to registry settings
619   CString strText;
620   WINDOWPLACEMENT wndpl;
621   CRect rect;
622   
623   strText = GetProfileString(strKey,s_profileRect);
624   if (!strText.IsEmpty()) {
625     rect.left = _ttoi((LPCTSTR ) strText);
626     rect.top =  _ttoi((LPCTSTR ) strText + 5);
627     rect.right = _ttoi((LPCTSTR ) strText + 10);
628     rect.bottom = _ttoi((LPCTSTR ) strText + 15);
629   } else {
630     rect = rcDefault;
631   }
632   
633   CRect rcMax;
634   SystemParametersInfo(SPI_GETWORKAREA, 0, (PVOID)(RECT *)rcMax, 0);
635   
636   if(rect.Width()<100 || rect.Height()<100 || rect.Width()>rcMax.Width() || rect.Height()>rcMax.Height()){
637     rect=rcDefault;
638   }
639   
640   wndpl.length = sizeof(WINDOWPLACEMENT);
641   wndpl.showCmd = GetProfileInt(strKey,_T("Show"),SW_SHOWNA);
642   wndpl.flags = 0;
643   
644   wndpl.ptMinPosition = CPoint(0, 0);
645   wndpl.ptMaxPosition =CPoint(-::GetSystemMetrics(SM_CXBORDER),-::GetSystemMetrics(SM_CYBORDER));
646   wndpl.rcNormalPosition = rect;
647   
648   // sets window's position and iconized/maximized status
649   return pWnd->SetWindowPlacement(&wndpl);
650 }
651
652 void CConfigToolApp::LoadFont(const CString & strKey, LOGFONT & lf)
653 {
654   lf.lfHeight                   =(LONG)GetProfileInt(strKey,_T("Height"),0);
655   if(0==lf.lfHeight){
656     CFont font;
657     font.Attach(HFONT(GetStockObject(DEFAULT_GUI_FONT)));
658     font.GetLogFont(&lf);
659   } else {
660     lf.lfWidth                  =(LONG)GetProfileInt(strKey,_T("Width"),0);
661     lf.lfEscapement             =(LONG)GetProfileInt(strKey,_T("Escapement"),0);
662     lf.lfOrientation    =(LONG)GetProfileInt(strKey,_T("Orientation"),0);
663     lf.lfWeight                 =(LONG)GetProfileInt(strKey,_T("Weight"),0);
664     lf.lfItalic                 =(BYTE)GetProfileInt(strKey,_T("Italic"),0);
665     lf.lfUnderline              =(BYTE)GetProfileInt(strKey,_T("Underline"),0);
666     lf.lfStrikeOut              =(BYTE)GetProfileInt(strKey,_T("StrikeOut"),0);
667     lf.lfCharSet                =(BYTE)GetProfileInt(strKey,_T("CharSet"),0);
668     lf.lfOutPrecision   =(BYTE)GetProfileInt(strKey,_T("OutPrecision"),0);
669     lf.lfClipPrecision  =(BYTE)GetProfileInt(strKey,_T("ClipPrecision"),0); 
670     lf.lfQuality                =(BYTE)GetProfileInt(strKey,_T("Quality"),0);
671     lf.lfPitchAndFamily =(BYTE)GetProfileInt(strKey,_T("PitchAndFamily"),0);
672     CString strFaceName =GetProfileString(strKey,_T("FaceName"),_T(""));
673     if(strFaceName.GetLength()<=31){
674       _tcscpy(lf.lfFaceName,strFaceName);
675     } else {
676       lf.lfFaceName[0]=_TCHAR('\0');
677     }
678   }
679 }
680
681 void CConfigToolApp::SaveFont(const CString & strKey, const LOGFONT & lf)
682 {
683   WriteProfileInt(strKey,_T("Height"),          (int)lf.lfHeight);
684   WriteProfileInt(strKey,_T("Width"),                   (int)lf.lfWidth);
685   WriteProfileInt(strKey,_T("Escapement"),      (int)lf.lfEscapement);
686   WriteProfileInt(strKey,_T("Orientation"),     (int)lf.lfOrientation);
687   WriteProfileInt(strKey,_T("Weight"),          (int)lf.lfWeight);
688   WriteProfileInt(strKey,_T("Italic"),          (int)lf.lfItalic);                      
689   WriteProfileInt(strKey,_T("Underline"),               (int)lf.lfUnderline);
690   WriteProfileInt(strKey,_T("StrikeOut"),               (int)lf.lfStrikeOut);   
691   WriteProfileInt(strKey,_T("CharSet"),         (int)lf.lfCharSet);                     
692   WriteProfileInt(strKey,_T("OutPrecision"),    (int)lf.lfOutPrecision);
693   WriteProfileInt(strKey,_T("ClipPrecision"),   (int)lf.lfClipPrecision);
694   WriteProfileInt(strKey,_T("Quality"),         (int)lf.lfQuality);     
695   WriteProfileInt(strKey,_T("PitchAndFamily"),(int)lf.lfPitchAndFamily);
696   WriteProfileString(strKey,_T("FaceName"),   lf.lfFaceName);
697 }
698
699 CEditView             *CConfigTool::m_pEditView=NULL;
700 COutputView         *CConfigTool::m_pOutputView=NULL;
701 CControlView    *CConfigTool::m_pControlView=NULL;
702 CCellView       *CConfigTool::m_pCellView=NULL;
703 CDescView       *CConfigTool::m_pDescView=NULL;
704 CPropertiesView *CConfigTool::m_pPropertiesView=NULL;
705 CRulesView      *CConfigTool::m_pRulesView=NULL;
706 CConfigToolDoc  *CConfigTool::m_pConfigToolDoc=NULL;
707 CMainFrame      *CConfigTool::m_pMain=NULL;
708 CMLTView        *CConfigTool::m_pMLTView=NULL;
709
710 CControlView    *CConfigTool::GetControlView()      { return m_pControlView; }
711 COutputView     *CConfigTool::GetOutputView()       { return m_pOutputView; }
712 CCellView       *CConfigTool::GetCellView()         { return m_pCellView; }
713 CDescView       *CConfigTool::GetDescView()         { return m_pDescView; }
714 CPropertiesView *CConfigTool::GetPropertiesView()   { return m_pPropertiesView; }
715 CRulesView      *CConfigTool::GetRulesView()        { return m_pRulesView; }
716 CMLTView        *CConfigTool::GetMLTView()          { return m_pMLTView; }
717
718 void CConfigTool::SetDocument(CConfigToolDoc *pDoc) {m_pConfigToolDoc=pDoc; }
719 void CConfigTool::SetMain(CMainFrame * pMain) { m_pMain=pMain; }
720 void CConfigTool::SetControlView(CControlView *pControlView) {m_pControlView=pControlView; }
721 void CConfigTool::SetOutputView(COutputView *pOutputView) {m_pOutputView=pOutputView; }
722 void CConfigTool::SetCellView(CCellView *pCellView) {m_pCellView=pCellView; }
723 void CConfigTool::SetDescView(CDescView *pDescView) {m_pDescView=pDescView; }
724 void CConfigTool::SetMLTView(CMLTView *pMLTView) {m_pMLTView=pMLTView; }
725 void CConfigTool::SetPropertiesView(CPropertiesView *pPropertiesView) {m_pPropertiesView=pPropertiesView; }
726 void CConfigTool::SetRulesView(CRulesView *pRulesView) {m_pRulesView=pRulesView; }
727 CMainFrame *CConfigTool::GetMain()
728 {
729   CWnd *pWnd=AfxGetMainWnd();
730   return (pWnd && pWnd->IsKindOf(RUNTIME_CLASS(CMainFrame)))?(CMainFrame*)pWnd:NULL;
731 }
732
733 int CConfigTool::Log(LPCTSTR pszFormat, ...)
734 {
735   va_list marker;
736   va_start (marker, pszFormat);
737   
738   for(int nLength=100;nLength;) {
739     TCHAR *buf=new TCHAR[1+nLength];
740     int n=_vsntprintf(buf, nLength, pszFormat, marker ); 
741     if(-1==n){
742       nLength*=2;  // NT behavior
743     } else if (n<nLength){
744       CWnd *pMain=CConfigTool::GetMain();
745       if(pMain){
746         // During startup the main window will be the splash screen
747         COutputView *pView=CConfigTool::GetOutputView();
748         if(pView){
749           pView->AddText(buf);
750           pView->AddText(_T("\r\n"));
751         }
752       }
753       ((CConfigToolApp *)AfxGetApp())->m_strBufferedLogMessages+=buf;
754       ((CConfigToolApp *)AfxGetApp())->m_strBufferedLogMessages+=_T("\r\n");
755       nLength=0;   // trigger exit from loop
756     } else {
757       nLength=n+1; // UNIX behavior generally, or NT behavior when buffer size exactly matches required length
758     }
759     delete [] buf;
760   }
761   
762   va_end (marker);
763   
764   return 0;
765 }
766
767 void CConfigTool::DismissSplash()
768 {
769   CSplashWnd::EnableSplashScreen(FALSE);
770 }
771
772 CString CConfigToolApp::GetInstallVersionKey ()
773 {
774   CString strKey = _T("SOFTWARE\\Red Hat\\eCos");
775   CString strVersionKey = _T("");
776   CString rc = _T("");
777   TCHAR pszBuffer [MAX_PATH + 1];
778   HKEY hKey;
779   
780   // get the greatest eCos version subkey
781   if (ERROR_SUCCESS == RegOpenKeyEx (HKEY_LOCAL_MACHINE, strKey, 0L, KEY_READ, &hKey)) {
782     DWORD dwIndex = 0;
783     while (ERROR_SUCCESS == RegEnumKey (hKey, dwIndex++, (LPTSTR) pszBuffer, sizeof (pszBuffer))) {
784       if (strVersionKey.Compare (pszBuffer) < 0) {
785         strVersionKey = pszBuffer;
786       }
787     }
788     RegCloseKey (hKey);
789   }
790   return strKey + _T("\\") + strVersionKey;
791 }