1 //####COPYRIGHTBEGIN####
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
5 // Copyright (C) 2003 John Dallaway
7 // This program is part of the eCos host tools.
9 // This program is free software; you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by the Free
11 // Software Foundation; either version 2 of the License, or (at your option)
14 // This program is distributed in the hope that it will be useful, but WITHOUT
15 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 // You should have received a copy of the GNU General Public License along with
20 // this program; if not, write to the Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 // ----------------------------------------------------------------------------
25 //####COPYRIGHTEND####
28 //===========================================================================
29 //#####DESCRIPTIONBEGIN####
31 // Author(s): julians, jld
32 // Contact(s): julians, jld
36 // Description: Implementation file for the ConfigTool application class
43 //####DESCRIPTIONEND####
45 //===========================================================================
47 // ============================================================================
49 // ============================================================================
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 #pragma implementation "configtool.h"
58 // Includes other headers for precompiled compilation
65 #include "eCosSocket.h"
66 #include "eCosTestPlatform.h"
68 #include "wx/splash.h"
69 #include "wx/cshelp.h"
71 #include "wx/filesys.h"
72 #include "wx/fs_zip.h"
73 #include "wx/config.h"
74 #include "wx/cmdline.h"
75 #include "wx/process.h"
76 #include "wx/mimetype.h"
77 #include "wx/txtstrm.h"
78 #include "wx/wfstream.h"
79 #include "wx/fs_mem.h"
81 #include "configtool.h"
82 #include "configtoolview.h"
83 #include "configtree.h"
85 #include "outputwin.h"
86 #include "configtooldoc.h"
88 #include "shortdescrwin.h"
89 #include "conflictwin.h"
90 #include "propertywin.h"
93 #include "Subprocess.h"
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
98 // the application icon
99 #if defined(__WXGTK__) || defined(__WXMOTIF__)
100 #include "bitmaps/configtool.xpm"
103 // Create a new application object.
106 BEGIN_EVENT_TABLE(ecApp, wxApp)
107 // Don't handle automatically, or it will bypass more specific processing.
108 // EVT_MENU(ecID_WHATS_THIS, ecApp::OnWhatsThis)
111 bool ecApp::sm_arMounted[26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
113 static const wxCmdLineEntryDesc sg_cmdLineDesc[] =
116 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
117 { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" },
119 { wxCMD_LINE_OPTION, "o", "output", "output file" },
120 { wxCMD_LINE_OPTION, "i", "input", "input dir" },
121 { wxCMD_LINE_OPTION, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER },
122 { wxCMD_LINE_OPTION, "d", "date", "output file date", wxCMD_LINE_VAL_DATE },
124 { wxCMD_LINE_SWITCH, "h", "help", "displays help on the command line parameters" },
125 { wxCMD_LINE_SWITCH, "e", "edit-only", "edit save file only" },
126 { wxCMD_LINE_SWITCH, "v", "version", "print version" },
127 { wxCMD_LINE_SWITCH, "c", "compile-help", "compile online help only" },
129 { wxCMD_LINE_PARAM, NULL, NULL, "input file 1", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
130 { wxCMD_LINE_PARAM, NULL, NULL, "input file 2", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
140 m_whatsThisMenu = new wxMenu;
141 m_whatsThisMenu->Append(ecID_WHATS_THIS, _("&What's This?"));
142 m_helpFile = wxEmptyString;
143 m_splashScreen = NULL;
144 m_pipedProcess = NULL;
146 m_helpController = NULL;
147 m_fileSystem = new wxFileSystem;
148 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
149 m_zipHandler = new wxZipFSHandler;
155 delete m_whatsThisMenu;
159 // 'Main program' equivalent: the program execution "starts" here
162 wxLog::SetTimestamp(NULL);
165 CeCosTestPlatform::Load();
167 wxHelpProvider::Set(new wxSimpleHelpProvider);
168 //wxHelpProvider::Set(new wxHelpControllerHelpProvider(& m_helpController));
170 wxImage::AddHandler(new wxPNGHandler);
171 wxImage::AddHandler(new wxGIFHandler);
173 // Required for advanced HTML help
174 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
175 wxFileSystem::AddHandler(m_zipHandler);
178 // Mandatory initialisation for Tcl 8.4
179 Tcl_FindExecutable(argv[0]);
181 wxString currentDir = wxGetCwd();
183 // Use argv to get current app directory
184 m_appDir = wxFindAppPath(argv[0], currentDir, wxT("CONFIGTOOLDIR"));
186 // If the development version, go up a directory.
188 if ((m_appDir.Right(5).CmpNoCase("DEBUG") == 0) ||
189 (m_appDir.Right(11).CmpNoCase("DEBUGSTABLE") == 0) ||
190 (m_appDir.Right(7).CmpNoCase("RELEASE") == 0) ||
191 (m_appDir.Right(13).CmpNoCase("RELEASESTABLE") == 0)
193 m_appDir = wxPathOnly(m_appDir);
196 // Load resources from binary resources archive, or failing that, from
197 // Windows resources or plain files
200 wxGetEnv(wxT("PATH"), & m_strOriginalPath);
202 // Create a document manager
203 m_docManager = new wxDocManager;
205 // Create a template relating documents to their views
206 wxDocTemplate* templ = new wxDocTemplate(m_docManager, "Configtool", "*.ecc", "", "ecc", "Configtool Doc", "Configtool View",
207 CLASSINFO(ecConfigToolDoc), CLASSINFO(ecConfigToolView));
209 // If we've only got one window, we only get to edit
210 // one document at a time.
211 m_docManager->SetMaxDocsOpen(1);
213 // Initialize config settings
216 // Let wxWindows know what the app name is
217 SetAppName(m_settings.GetAppName());
219 InitializeWindowSettings(TRUE /* beforeWindowConstruction */) ;
221 // Load config settings
222 m_settings.LoadConfig();
224 // Set the default directory for opening/saving files
225 if (!m_settings.m_lastFilename.IsEmpty())
226 templ->SetDirectory(wxPathOnly(m_settings.m_lastFilename));
228 // Parse the command-line parameters and options
229 wxCmdLineParser parser(sg_cmdLineDesc, argc, argv);
233 res = parser.Parse();
235 if (res == -1 || res > 0 || parser.Found(wxT("h")))
238 wxLog::SetActiveTarget(new wxLogStderr);
243 if (parser.Found(wxT("v")))
246 wxLog::SetActiveTarget(new wxLogStderr);
249 msg.Printf(wxT("eCos Configuration Tool (c) Red Hat, 2001 Version %.2f, %s"), ecCONFIGURATION_TOOL_VERSION, __DATE__);
256 wxBitmap bitmap(wxBITMAP(splash));
259 if (wxFileExists("splash16.png"))
260 bitmap.LoadFile("splash16.png", wxBITMAP_TYPE_PNG);
264 // wxYieldIfNeeded();
266 // create the main application window
267 ecMainFrame *frame = new ecMainFrame(m_docManager, GetSettings().GetAppName(),
268 wxPoint(GetSettings().m_frameSize.x, GetSettings().m_frameSize.y),
269 wxSize(GetSettings().m_frameSize.width, GetSettings().m_frameSize.height));
274 SendIdleEvents(); // Otherwise UI updates aren't done, because it's busy loading the repository
276 ::UpdateWindow((HWND) frame->GetHWND());
279 InitializeWindowSettings(FALSE /* beforeWindowConstruction */) ;
281 if (m_splashScreenBitmap.Ok() && GetSettings().m_showSplashScreen)
283 m_splashScreen = new ecSplashScreen(m_splashScreenBitmap, wxSPLASH_CENTRE_ON_SCREEN|wxSPLASH_TIMEOUT,
284 5000, NULL, -1, wxDefaultPosition, wxDefaultSize, wxNO_BORDER|wxFRAME_FLOAT_ON_PARENT|wxSTAY_ON_TOP);
287 // if (m_splashScreen)
288 // m_splashScreen->Raise();
292 if (parser.Found(wxT("e")))
293 GetSettings().m_editSaveFileOnly = TRUE;
295 // If in --compile-help (-c) mode, then exit immediately after recompiling help file
296 bool compileHelpOnly = parser.Found(wxT("c"));
298 wxString filenameToOpen1, filenameToOpen2;
299 if (parser.GetParamCount() > 0)
301 wxString tmpSaveFile;
303 filenameToOpen1 = parser.GetParam(0);
305 if (parser.GetParamCount() > 1)
306 filenameToOpen2 = parser.GetParam(1);
308 bool gotRepository = FALSE;
309 bool gotSavefile = FALSE;
311 wxString repositoryDir, saveFile;
313 // Might be e.g. . or .. in path, or relative path
314 filenameToOpen1 = wxGetRealPath(currentDir, filenameToOpen1);
316 if (parser.GetParamCount() > 1)
317 filenameToOpen2 = wxGetRealPath(currentDir, filenameToOpen2);
319 wxString path1, name1, ext1;
320 wxSplitPath(filenameToOpen1, & path1, & name1, & ext1);
322 wxString path2, name2, ext2;
323 wxSplitPath(filenameToOpen2, & path2, & name2, & ext2);
325 // Look at the first file
326 if (ext1 == "ecc" || ext1 == "ECC")
328 if (wxFileExists(filenameToOpen1))
331 saveFile = filenameToOpen1;
336 msg.Printf("%s is not a valid file.", (const wxChar*) filenameToOpen1);
337 wxMessageBox(msg, wxGetApp().GetSettings().GetAppName(), wxICON_EXCLAMATION|wxOK);
341 else if (wxDirExists(filenameToOpen1) && FindSaveFileInDir(filenameToOpen1, tmpSaveFile))
343 saveFile = tmpSaveFile;
346 else if ((name1 == wxT("ecos") && ext1 == wxT("db") && wxFileExists(filenameToOpen1)) || wxDirExists(filenameToOpen1))
348 // It's a repository (we hope).
349 if (name1 == wxT("ecos") && ext1 == wxT("db"))
352 filenameToOpen1 = wxPathOnly(filenameToOpen1);
353 filenameToOpen1 = wxPathOnly(filenameToOpen1);
357 // If it's the packages directory, we need to strip off
359 wxString eccPath(filenameToOpen1 + wxString(wxFILE_SEP_PATH) + wxT("ecc"));
361 // Don't strip off ecc if it's the CVS repository (with ecc below it)
362 if (name1 == wxT("packages") || (name1 == wxT("ecc") && !wxDirExists(eccPath)))
363 filenameToOpen1 = wxPathOnly(filenameToOpen1);
366 repositoryDir = filenameToOpen1;
367 gotRepository = TRUE;
372 msg.Printf("%s is neither a project file nor a valid repository.", (const wxChar*) filenameToOpen1);
373 wxMessageBox(msg, wxGetApp().GetSettings().GetAppName(), wxICON_EXCLAMATION|wxOK);
377 // Look at the second file, if any
378 if (parser.GetParamCount() > 1)
380 if (ext2 == "ecc" || ext2 == "ECC")
382 if (wxFileExists(filenameToOpen2))
387 msg.Printf("%s is a second save file -- please supply only one.", (const wxChar*) filenameToOpen2);
388 wxMessageBox(msg, wxGetApp().GetSettings().GetAppName(), wxICON_EXCLAMATION|wxOK);
393 saveFile = filenameToOpen2;
398 msg.Printf("%s is not a valid file.", (const wxChar*) filenameToOpen2);
399 wxMessageBox(msg, wxGetApp().GetSettings().GetAppName(), wxICON_EXCLAMATION|wxOK);
403 else if (!gotSavefile && wxDirExists(filenameToOpen2) && FindSaveFileInDir(filenameToOpen2, tmpSaveFile))
405 saveFile = tmpSaveFile;
408 else if ((name2 == wxT("ecos") && ext2 == wxT("db") && wxFileExists(filenameToOpen2)) || wxDirExists(filenameToOpen2))
413 msg.Printf("%s is a second repository -- please supply only one.", (const wxChar*) filenameToOpen2);
414 wxMessageBox(msg, wxGetApp().GetSettings().GetAppName(), wxICON_EXCLAMATION|wxOK);
418 // It's a repository (we hope).
419 if (name1 == wxT("ecos") && ext1 == wxT("db"))
422 filenameToOpen2 = wxPathOnly(filenameToOpen2);
423 filenameToOpen2 = wxPathOnly(filenameToOpen2);
427 // If it's the packages directory, we need to strip off
429 wxString eccPath(filenameToOpen2 + wxString(wxFILE_SEP_PATH) + wxT("ecc"));
431 // Don't strip off ecc if it's the CVS repository (with ecc below it)
432 if (name2 == wxT("packages") || (name2 == wxT("ecc") && !wxDirExists(eccPath)))
433 filenameToOpen2 = wxPathOnly(filenameToOpen2);
436 repositoryDir = filenameToOpen2;
437 gotRepository = TRUE;
442 msg.Printf("%s is neither a project file nor a valid repository.", (const wxChar*) filenameToOpen2);
443 wxMessageBox(msg, wxGetApp().GetSettings().GetAppName(), wxICON_EXCLAMATION|wxOK);
448 // Now we have looked at the two files and decided what they are; let's
453 GetSettings().m_strRepository = repositoryDir;
458 // See if there's a save file in the current dir
459 if (FindSaveFileInDir(currentDir, saveFile))
465 // The repository will be loaded from m_strRepository, possibly set above.
466 m_docManager->CreateDocument(saveFile, wxDOC_SILENT);
471 m_docManager->CreateDocument(wxString(""), wxDOC_NEW);
479 msg.Printf(wxT("Please specify a repository when using the --compile-help option."));
480 wxMessageBox(msg, wxGetApp().GetSettings().GetAppName(), wxICON_EXCLAMATION|wxOK);
483 ecConfigToolDoc* doc = wxGetApp().GetConfigToolDoc();
486 if (!doc->RebuildHelpIndex(TRUE))
489 msg.Printf(wxT("Sorry, there was a problem compiling the help index."));
490 wxMessageBox(msg, wxGetApp().GetSettings().GetAppName(), wxICON_EXCLAMATION|wxOK);
497 msg.Printf(wxT("Sorry, there was no current document when compiling the help index."));
498 wxMessageBox(msg, wxGetApp().GetSettings().GetAppName(), wxICON_EXCLAMATION|wxOK);
502 // Return FALSE in order to quit the application
508 m_docManager->CreateDocument(wxString(""), wxDOC_NEW);
514 // Load resources from disk
515 bool ecApp::LoadResources()
517 wxFileSystem::AddHandler(new wxMemoryFSHandler);
519 // LoadBitmapResource(m_splashScreenBitmap, wxT("splash16.png"), wxBITMAP_TYPE_PNG, FALSE);
521 // wxBitmap bitmap1, bitmap2, bitmap3;
522 // LoadBitmapResource(bitmap1, wxT("ecoslogo.png"), wxBITMAP_TYPE_PNG, TRUE);
523 // LoadBitmapResource(bitmap2, wxT("ecoslogosmall.png"), wxBITMAP_TYPE_PNG, TRUE);
524 // LoadBitmapResource(bitmap3, wxT("rhlogo.png"), wxBITMAP_TYPE_PNG, TRUE);
526 // wxString aboutText;
527 // LoadTextResource(aboutText, wxT("about.htm"), TRUE);
529 // VersionStampSplashScreen();
534 // Load resources from zip resource file or disk
535 bool ecApp::LoadBitmapResource(wxBitmap& bitmap, const wxString& filename, int bitmapType, bool addToMemoryFS)
537 wxString archive(GetFullAppPath(wxT("configtool.bin")));
539 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
540 wxFSFile* file = m_fileSystem->OpenFile(archive + wxString(wxT("#zip:")) + filename);
543 wxInputStream* stream = file->GetStream();
545 wxImage image(* stream, bitmapType);
546 bitmap = image.ConvertToBitmap();
554 bitmap.LoadFile(filename, wxBITMAP_TYPE_BMP_RESOURCE);
557 if (!bitmap.Ok() && wxFileExists(GetFullAppPath(filename)))
558 bitmap.LoadFile(GetFullAppPath(filename), bitmapType);
561 if (bitmap.Ok() && addToMemoryFS)
562 wxMemoryFSHandler::AddFile(filename, bitmap, bitmapType);
567 // Load resources from zip resource file or disk
568 bool ecApp::LoadTextResource(wxString& text, const wxString& filename, bool addToMemoryFS)
570 wxString archive(GetFullAppPath(wxT("configtool.bin")));
572 bool success = FALSE;
574 #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
575 wxFSFile* file = m_fileSystem->OpenFile(archive + wxString(wxT("#zip:")) + filename);
578 wxInputStream* stream = file->GetStream();
580 char* buf = text.GetWriteBuf(stream->GetSize() + 1);
581 stream->Read((void*) buf, stream->GetSize());
582 buf[stream->GetSize()] = 0;
583 text.UngetWriteBuf();
591 if (!success && wxFileExists(GetFullAppPath(filename)))
593 wxFileInputStream str(GetFullAppPath(filename));
595 char* buf = text.GetWriteBuf(str.GetSize() + 1);
596 str.Read((void*) buf, str.GetSize());
597 buf[str.GetSize()] = 0;
598 text.UngetWriteBuf();
603 if (success && addToMemoryFS)
604 wxMemoryFSHandler::AddFile(filename, text);
609 // Get a text resource from the memory filesystem
610 bool ecApp::GetMemoryTextResource(const wxString& filename, wxString& text)
612 wxString s(wxString(wxT("memory:")) + filename);
613 wxFSFile* file = wxGetApp().GetFileSystem()->OpenFile(s);
616 wxInputStream* stream = file->GetStream();
618 char* buf = text.GetWriteBuf(stream->GetSize() + 1);
619 stream->Read((void*) buf, stream->GetSize());
620 buf[stream->GetSize()] = 0;
621 text.UngetWriteBuf();
630 // Version-stamp the splash screen
631 bool ecApp::VersionStampSplashScreen()
633 if (m_splashScreenBitmap.Ok())
636 dc.SelectObject(m_splashScreenBitmap);
638 wxColour textColour(19, 49, 4);
639 dc.SetTextForeground(textColour);
640 dc.SetBackgroundMode(wxTRANSPARENT);
641 dc.SetFont(wxFont(11, wxSWISS, wxNORMAL, wxBOLD, FALSE));
643 // Bottom left of area to start drawing at
646 verString.Printf("%.2f", ecCONFIGURATION_TOOL_VERSION);
648 int x = 339; int y = 231;
650 y += 5; // For some reason
653 dc.GetTextExtent(verString, & w, & h);
654 dc.DrawText(verString, x, y - h);
656 dc.SelectObject(wxNullBitmap);
664 // Initialize window settings object
665 bool ecApp::InitializeWindowSettings(bool beforeWindowConstruction)
667 wxWindowSettings& settings = GetSettings().m_windowSettings;
668 ecMainFrame* frame = GetMainFrame();
670 if (beforeWindowConstruction)
672 settings.Add(wxT("Configuration"));
673 settings.Add(wxT("Short Description"));
674 settings.Add(wxT("Output"));
675 settings.Add(wxT("Properties"));
676 settings.Add(wxT("Conflicts"));
681 arr.Add(frame->GetTreeCtrl());
682 arr.Add(frame->GetValueWindow());
683 settings.SetWindows(wxT("Configuration"), arr);
685 settings.SetWindow(wxT("Short Description"), frame->GetShortDescriptionWindow());
686 settings.SetWindow(wxT("Output"), frame->GetOutputWindow());
687 settings.SetWindow(wxT("Properties"), frame->GetPropertyListWindow());
688 settings.SetWindow(wxT("Conflicts"), frame->GetConflictsWindow());
695 bool ecApp::InitializeHelpController()
697 if (m_helpController)
698 delete m_helpController;
699 m_helpController = new wxHelpController;
701 if (!m_helpController->Initialize(GetHelpFile()))
708 // Tell the help controller where the repository documentation is.
709 // For now, just keep this to myself since it uses hacks to wxWin
710 ecConfigToolDoc* doc = GetConfigToolDoc();
712 // No longer using relative paths
716 wxString htmlDir = wxString(doc->GetRepository()) + wxString(wxT("/doc/html"));
717 if (!wxDirExists(htmlDir))
718 htmlDir = wxString(doc->GetRepository()) + wxString(wxT("/doc"));
720 htmlDir += wxString(wxT("/"));
722 wxGetApp().GetHelpController().SetBookBasePath(htmlDir);
729 // Check if there is a (unique) .ecc file in dir
730 bool ecApp::FindSaveFileInDir(const wxString& dir, wxString& saveFile)
734 if (!fileFind.Open(dir))
737 wxString wildcard = wxT(".ecc");
740 bool found = fileFind.GetFirst (& filename, wildcard);
743 // Fail if there was more than one matching file.
745 if (fileFind.GetNext (& filename2))
749 saveFile = dir + wxString(wxFILE_SEP_PATH) + filename;
756 int ecApp::OnExit(void)
759 if (m_helpController)
761 delete m_helpController;
762 m_helpController = NULL;
768 m_splashScreen->Destroy();
769 m_splashScreen = NULL;
772 m_settings.SaveConfig();
775 wxConfig config(wxGetApp().GetSettings().GetConfigAppName());
776 if (config.HasGroup(wxT("FileHistory")))
777 config.DeleteGroup(wxT("FileHistory"));
778 config.SetPath(wxT("FileHistory/"));
779 m_docManager->FileHistorySave(config);
787 // Prepend the current program directory to the name
788 wxString ecApp::GetFullAppPath(const wxString& filename) const
790 wxString path(m_appDir);
791 if (path.Last() != '\\' && path.Last() != '/' && filename[0] != '\\' && filename[0] != '/')
802 // Are we running in 32K colours or more?
803 bool ecApp::GetHiColour() const
805 static bool hiColour = (wxDisplayDepth() >= 16) ;
809 // General handler for 'What's this?'
810 void ecApp::OnWhatsThis(wxCommandEvent& event)
812 wxObject* obj = event.GetEventObject();
813 wxWindow* win = NULL;
815 if (obj->IsKindOf(CLASSINFO(wxMenu)))
817 win = ((wxMenu*) obj)->GetInvokingWindow();
819 else if (obj->IsKindOf(CLASSINFO(wxWindow)))
821 win = (wxWindow*) obj;
824 wxWindow* subjectOfHelp = win;
825 bool eventProcessed = FALSE;
826 wxPoint pt = wxGetMousePosition();
828 while (subjectOfHelp && !eventProcessed)
830 wxHelpEvent helpEvent(wxEVT_HELP, subjectOfHelp->GetId(), pt) ;
831 helpEvent.SetEventObject(this);
832 eventProcessed = win->GetEventHandler()->ProcessEvent(helpEvent);
834 // Go up the window hierarchy until the event is handled (or not).
835 // I.e. keep submitting ancestor windows until one is recognised
836 // by the app code that processes the ids and displays help.
837 subjectOfHelp = subjectOfHelp->GetParent();
839 // wxGetApp().GetHelpController().DisplayTextPopup(GetHelpText(), wxGetMousePosition());
842 // Log to output window
843 void ecApp::Log(const wxString& msg)
845 ecMainFrame* frame = (ecMainFrame*) GetTopWindow();
848 frame->GetOutputWindow()->AppendText(msg /* + wxT("\n") */ );
849 if ((msg == wxEmptyString) || (msg.Last() != wxT('\n')))
850 frame->GetOutputWindow()->AppendText(wxT("\n"));
852 frame->GetOutputWindow()->ShowPosition(frame->GetOutputWindow()->GetLastPosition());
856 void ecApp::SetStatusText(const wxString& text, bool clearFailingRulesPane)
858 ecMainFrame* mainFrame = GetMainFrame();
861 mainFrame->GetStatusBar()->SetStatusText(text, ecStatusPane);
862 if (clearFailingRulesPane)
863 mainFrame->GetStatusBar()->SetStatusText(wxT(""), ecFailRulePane);
865 ::UpdateWindow((HWND) mainFrame->GetHWND());
871 // Config tree control
872 ecConfigTreeCtrl* ecApp::GetTreeCtrl() const
874 return GetMainFrame()->GetTreeCtrl();
878 ecMemoryLayoutWindow* ecApp::GetMLTWindow() const
880 return GetMainFrame()->GetMemoryLayoutWindow();
883 // Get active document
884 ecConfigToolDoc* ecApp::GetConfigToolDoc() const
892 return wxDynamicCast(m_docManager->GetCurrentDocument(), ecConfigToolDoc);
895 bool ecApp::Launch(const wxString & strFileName,const wxString &strViewer)
900 if (!strViewer.IsEmpty())
902 cmd = strViewer + wxString(wxT(" ")) + strFileName ;
906 wxString path, filename, ext;
907 wxSplitPath(strFileName, & path, & filename, & ext);
909 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
912 wxLogError(_T("Impossible to determine the file type for extension '%s'"),
917 bool ok = ft->GetOpenCommand(&cmd,
918 wxFileType::MessageParameters(strFileName, _T("")));
923 // TODO: some kind of configuration dialog here.
924 wxMessageBox(_("Could not determine the command for opening this file."),
925 wxGetApp().GetSettings().GetAppName(), wxOK|wxICON_EXCLAMATION);
930 ok = (wxExecute(cmd, FALSE) != 0);
937 if(!strViewer.IsEmpty())//use custom editor
939 CString strCmdline(strViewer);
941 TCHAR *pszCmdLine=strCmdline.GetBuffer(strCmdline.GetLength());
942 GetShortPathName(pszCmdLine,pszCmdLine,strCmdline.GetLength());
943 strCmdline.ReleaseBuffer();
945 strCmdline+=_TCHAR(' ');
946 strCmdline+=strFileName;
947 PROCESS_INFORMATION pi;
950 si.cb = sizeof(STARTUPINFO);
951 si.lpReserved = NULL;
952 si.lpReserved2 = NULL;
960 //strCmdline.GetBuffer(strCmdline.GetLength()), // command line
961 strCmdline.GetBuffer(strCmdline.GetLength()), // command line
962 NULL, // process security
963 NULL, // thread security
964 TRUE, // inherit handles
970 CloseHandle(pi.hProcess);
971 CloseHandle(pi.hThread);
974 CUtils::MessageBoxF(_T("Failed to invoke %s.\n"),strCmdline);
976 strCmdline.ReleaseBuffer();
977 } else {// Use association
978 TCHAR szExe[MAX_PATH];
979 HINSTANCE h=FindExecutable(strFileName,_T("."),szExe);
983 case 0: str=_T("The system is out of memory or resources.");break;
984 case 31: str=_T("There is no association for the specified file type.");break;
985 case ERROR_FILE_NOT_FOUND: str=_T("The specified file was not found.");break;
986 case ERROR_PATH_NOT_FOUND: str=_T("The specified path was not found.");break;
987 case ERROR_BAD_FORMAT: str=_T("The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).");break;
990 CUtils::MessageBoxF(_T("Failed to open document %s.\r\n%s"),strFileName,str);
993 SHELLEXECUTEINFO sei = {sizeof(sei), 0, AfxGetMainWnd()->GetSafeHwnd(), _T("open"),
994 strFileName, NULL, NULL, SW_SHOWNORMAL, AfxGetInstanceHandle( )};
997 HINSTANCE hInst=ShellExecute(AfxGetMainWnd()->GetSafeHwnd(),_T("open"), strFileName, NULL, _T("."), 0)/*ShellExecuteEx(&sei)*/;
998 if(int(hInst)<=32/*sei.hInstApp==0*/)
1003 case 0 : str=_T("The operating system is out of memory or resources. ");break;
1004 case ERROR_FILE_NOT_FOUND : str=_T("The specified file was not found. ");break;
1005 case ERROR_PATH_NOT_FOUND : str=_T("The specified path was not found. ");break;
1006 case ERROR_BAD_FORMAT : str=_T("The .EXE file is invalid (non-Win32 .EXE or error in .EXE image). ");break;
1007 case SE_ERR_ACCESSDENIED : str=_T("The operating system denied access to the specified file. ");break;
1008 case SE_ERR_ASSOCINCOMPLETE : str=_T("The filename association is incomplete or invalid. ");break;
1009 case SE_ERR_DDEBUSY : str=_T("The DDE transaction could not be completed because other DDE transactions were being processed. ");break;
1010 case SE_ERR_DDEFAIL : str=_T("The DDE transaction failed. ");break;
1011 case SE_ERR_DDETIMEOUT : str=_T("The DDE transaction could not be completed because the request timed out. ");break;
1012 case SE_ERR_DLLNOTFOUND : str=_T("The specified dynamic-link library was not found. ");break;
1013 //case SE_ERR_FNF : str=_T("The specified file was not found. ");break;
1014 case SE_ERR_NOASSOC : str=_T("There is no application associated with the given filename extension. ");break;
1015 case SE_ERR_OOM : str=_T("There was not enough memory to complete the operation. ");break;
1016 //case SE_ERR_PNF : str=_T("The specified path was not found. ");break;
1017 case SE_ERR_SHARE : str=_T("A sharing violation occurred. ");break;
1018 default: str=_T("An unexpected error occurred");break;
1020 CUtils::MessageBoxF(_T("Failed to open document %s using %s.\r\n%s"),strFileName,szExe,str);
1030 bool ecApp::PrepareEnvironment(bool bWithBuildTools, wxString* cmdLine)
1033 // Under Windows we can set variables.
1034 ecConfigToolDoc *pDoc = GetConfigToolDoc();
1036 wxSetEnv(wxT("PATH"), m_strOriginalPath);
1038 const wxString strPrefix(pDoc->GetCurrentTargetPrefix());
1039 ecFileName strBinDir;
1042 rc=(! bWithBuildTools) || GetSettings().m_arstrBinDirs.Find(strPrefix, strBinDir);
1045 // Use fallback of previously-entered build tools directory, if available
1046 if (!GetSettings().m_buildToolsDir.IsEmpty())
1048 strBinDir = GetSettings().m_buildToolsDir ;
1053 wxCommandEvent event;
1054 GetMainFrame()->OnBuildToolsPath(event);
1055 rc = GetSettings().m_arstrBinDirs.Find(strPrefix, strBinDir);
1061 ecFileName strUserBinDir(GetSettings().m_userToolsDir);
1062 if(strUserBinDir.IsEmpty())
1064 if ( 1 == GetSettings().m_userToolPaths.GetCount() )
1066 GetSettings().m_userToolsDir = GetSettings().m_userToolPaths[0];
1069 wxCommandEvent event;
1070 GetMainFrame()->OnUserToolsPath(event);
1072 strUserBinDir = GetSettings().m_userToolsDir;
1074 if ( !strUserBinDir.IsEmpty() )
1076 // calculate the directory of the host tools from this application's module name
1077 ecFileName strHostToolsBinDir(this->argv[0]);
1078 strHostToolsBinDir = strHostToolsBinDir.Head ();
1080 // tools directories are in the order host-tools, user-tools, comp-tools, install/bin (if present), contrib-tools (if present) on the path
1081 const ecFileName strContribBinDir(strUserBinDir, wxT("..\\contrib\\bin"));
1082 ecFileName strUsrBinDir(strUserBinDir, wxT("..\\usr\\bin"));
1083 const ecFileName strInstallBinDir(pDoc->GetInstallTree (), wxT("bin"));
1085 // In case strUserBinDir is e.g. c:\program files\red hat\cygwin-00r1\usertools\h-i686-pc-cygwin\bin
1086 if (!strUsrBinDir.IsDir ())
1087 strUsrBinDir = ecFileName(strUserBinDir + _T("..\\..\\..\\H-i686-pc-cygwin\\bin"));
1090 (strUsrBinDir.IsDir () && ! ecUtils::AddToPath (strUsrBinDir)) ||
1091 (strContribBinDir.IsDir () && ! ecUtils::AddToPath (strContribBinDir)) ||
1092 (strInstallBinDir.IsDir () && ! ecUtils::AddToPath (strInstallBinDir)) ||
1093 (bWithBuildTools && ! ecUtils::AddToPath (strBinDir)) ||
1094 ! ecUtils::AddToPath (strUserBinDir) ||
1095 ! ecUtils::AddToPath (strHostToolsBinDir))
1098 msg.Printf(wxT("Failed to set PATH environment variable"));
1099 wxMessageBox(msg, wxGetApp().GetSettings().GetAppName(), wxICON_EXCLAMATION|wxOK);
1103 if(!wxSetEnv(_T("MAKE_MODE"),_T("unix")))
1106 msg.Printf(wxT("Failed to set MAKE_MODE environment variable"));
1107 wxMessageBox(msg, wxGetApp().GetSettings().GetAppName(), wxICON_EXCLAMATION|wxOK);
1111 // Remove from the environment
1112 wxUnsetEnv(wxT("GDBTK_LIBRARY"));
1113 wxUnsetEnv(wxT("GCC_EXEC_PREFIX"));
1115 // Useful for ecosconfig
1116 wxSetEnv(wxT("ECOS_REPOSITORY"), pDoc->GetPackagesDir());
1118 // Mount /ecos-x so we can access these in text mode
1119 if (! pDoc->GetBuildTree().IsEmpty() && wxIsalpha(pDoc->GetBuildTree()[0]))
1120 CygMount(pDoc->GetBuildTree()[0]);
1121 if (! pDoc->GetInstallTree().IsEmpty() && wxIsalpha(pDoc->GetInstallTree()[0]))
1122 CygMount(pDoc->GetInstallTree()[0]);
1123 if (! pDoc->GetRepository().IsEmpty() && wxIsalpha(pDoc->GetRepository()[0]))
1124 CygMount(pDoc->GetRepository()[0]);
1131 wxASSERT ( cmdLine != NULL );
1133 (* cmdLine) = wxEmptyString;
1135 // Under Unix we need to build up a command line to set variables and invoke make
1136 ecConfigToolDoc *pDoc = GetConfigToolDoc();
1138 const wxString strPrefix(pDoc->GetCurrentTargetPrefix());
1139 ecFileName strBinDir;
1142 rc=(! bWithBuildTools) || GetSettings().m_arstrBinDirs.Find(strPrefix, strBinDir);
1145 // Use fallback of previously-entered build tools directory, if available
1146 if (!GetSettings().m_buildToolsDir.IsEmpty())
1148 strBinDir = GetSettings().m_buildToolsDir ;
1153 wxCommandEvent event;
1154 GetMainFrame()->OnBuildToolsPath(event);
1155 rc = GetSettings().m_arstrBinDirs.Find(strPrefix, strBinDir);
1161 if (!strBinDir.IsEmpty())
1163 (* cmdLine) += wxString(wxT("export PATH=")) + wxString(strBinDir) + wxT(":$PATH; ");
1165 // Also set the path
1166 wxString oldPath(wxGetenv(wxT("PATH")));
1167 wxString path(strBinDir);
1168 if (!oldPath.IsEmpty())
1173 wxSetEnv(wxT("PATH"), path);
1175 (* cmdLine) += wxString(wxT("unset GDBTK_LIBRARY; ")) ;
1176 wxUnsetEnv(wxT("GDBTK_LIBRARY"));
1178 (* cmdLine) += wxString(wxT("unset GCC_EXEC_PREFIX; ")) ;
1179 wxUnsetEnv(wxT("GCC_EXEC_PREFIX"));
1181 (* cmdLine) += wxString(wxT("export ECOS_REPOSITORY=")) + wxString(pDoc->GetPackagesDir()) + wxT("; ");
1182 wxSetEnv(wxT("ECOS_REPOSITORY"), pDoc->GetPackagesDir());
1185 ecFileName strUserBinDir(GetSettings().m_userToolsDir);
1186 if(strUserBinDir.IsEmpty())
1188 if ( 1 == GetSettings().m_userToolPaths.GetCount() )
1190 GetSettings().m_userToolsDir = GetSettings().m_userToolPaths[0];
1193 wxCommandEvent event;
1194 GetMainFrame()->OnUserToolsPath(event);
1196 strUserBinDir = GetSettings().m_userToolsDir;
1198 if ( !strUserBinDir.IsEmpty() )
1200 // calculate the directory of the host tools from this application's module name
1201 ecFileName strHostToolsBinDir(this->argv[0]);
1202 strHostToolsBinDir = strHostToolsBinDir.Head ();
1204 // tools directories are in the order host-tools, user-tools, comp-tools, install/bin (if present), contrib-tools (if present) on the path
1206 // TODO: is this right? Assuming that the user tools are already in the user's path.
1207 // const ecFileName strContribBinDir(strUserBinDir, wxT("..\\contrib\\bin"));
1208 // const ecFileName strUsrBinDir(strUserBinDir, wxT("..\\usr\\bin"));
1209 const ecFileName strInstallBinDir(pDoc->GetInstallTree (), wxT("bin"));
1211 (* cmdLine) += wxString(wxT("export PATH=")) + wxString(strInstallBinDir) + wxT(":$PATH; ");
1212 (* cmdLine) += wxString(wxT("unset GDBTK_LIBRARY; ")) ;
1213 (* cmdLine) += wxString(wxT("unset GCC_EXEC_PREFIX; ")) ;
1214 (* cmdLine) += wxString(wxT("export ECOS_REPOSITORY=")) + wxString(pDoc->GetPackagesDir()) + wxT("; ");
1222 void ecApp::CygMount(wxChar c)
1224 // May not be alpha if it's e.g. a UNC network path
1230 if(!sm_arMounted[c-_TCHAR('a')])
1232 sm_arMounted[c-wxChar('a')]=true;
1236 strCmd.Printf(wxT("mount %c: /ecos-%c"),c,c);
1238 sub.Run(strOutput,strCmd);
1242 // Doing it with wxExecute results in a flashing DOS box unfortunately
1244 wxASSERT(wxIsalpha(c));
1246 if(!sm_arMounted[c-wxChar('a')])
1248 sm_arMounted[c-wxChar('a')] = TRUE;
1251 strCmd.Printf(wxT("mount.exe %c: /%c"),c,c);
1253 wxExecute(strCmd, TRUE);
1258 // Fiddling directly with the registry DOESN'T WORK because Cygwin mount tables
1259 // get out of synch with the registry
1260 void ecApp::CygMountText(wxChar c)
1262 wxASSERT(wxIsalpha(c));
1264 // if(!sm_arMounted[c-wxChar('a')])
1266 // sm_arMounted[c-wxChar('a')] = TRUE;
1271 strCmd.Printf(wxT("mount.exe %c: /ecos-%c"),c,c);
1273 wxExecute(strCmd, TRUE);
1275 wxString key, value;
1276 key.Printf(wxT("/ecos-%c"), c);
1277 value.Printf(wxT("%c:"), c);
1279 // Mount by fiddling with registry instead, so we don't see ugly flashing windows
1282 if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Cygnus Solutions\\Cygwin\\mounts v2",
1283 0, KEY_READ, &hKey))
1288 if (ERROR_SUCCESS == RegCreateKeyEx(hKey, key, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
1289 NULL, & hSubKey, & disposition))
1291 RegSetValueEx(hSubKey, "native", 0, REG_SZ, (unsigned char*) (const wxChar*) value, value.Length() + 1);
1292 RegCloseKey(hSubKey);
1303 void ecApp::Build(const wxString &strWhat /*=wxT("")*/ )
1308 ecConfigToolDoc* pDoc = GetConfigToolDoc();
1312 if (!wxGetApp().GetMainFrame()->GetOutputWindow()->IsShown())
1314 wxGetApp().GetMainFrame()->ToggleWindow(ecID_TOGGLE_OUTPUT);
1316 if (!pDoc->GetDocumentSaved())
1321 if (pDoc->IsModified() && !wxDirExists(pDoc->GetBuildTree()))
1326 // if ( !(pDoc->IsModified() || !wxDirExists(pDoc->GetBuildTree())) ) // verify the save worked
1327 if ( pDoc->GetDocumentSaved() )
1329 //wxString strCmd (wxT("c:\\bin\\testmake.bat"));
1330 wxString strCmd (wxT("make"));
1331 if(!strWhat.IsEmpty())
1337 if(!GetSettings().m_strMakeOptions.IsEmpty())
1340 strCmd += GetSettings().m_strMakeOptions;
1342 strCmd += wxT(" --directory ");
1344 // Quoting the name may not mix with the 'sh' command on Unix, so only do it
1345 // under Windows where it's more likely there will be spaces needing quoting.
1347 wxString buildDir(pDoc->GetBuildTree());
1349 #if ecUSE_ECOS_X_NOTATION
1350 std::string cPath = cygpath(std::string(pDoc->GetBuildTree()));
1351 buildDir = cPath.c_str();
1353 strCmd += wxString(wxT("\"")) + buildDir + wxString(wxT("\""));
1355 strCmd += wxString(pDoc->GetBuildTree()) ;
1358 wxString variableSettings;
1360 if (PrepareEnvironment(TRUE, & variableSettings))
1363 // strCmd is all we need
1365 // strCmd has to invoke a shell with variables and make invocation
1366 strCmd = wxString(wxT("sh -c \"")) + variableSettings + strCmd + wxString(wxT("\""));
1368 // Output the command so we know what we're doing
1372 // No: pass --directory
1373 // wxSetWorkingDirectory(pDoc->GetBuildTree());
1375 m_pipedProcess = new ecPipedProcess;
1376 long pid = wxExecute(strCmd, FALSE, m_pipedProcess);
1379 m_pipedProcess->SetPid(pid);
1380 // wxLogStatus(_T("Process %ld (%s) launched."), pid, cmd.c_str());
1384 wxLogError(_T("Execution of '%s' failed."), strCmd.c_str());
1386 delete m_pipedProcess;
1387 m_pipedProcess = NULL;
1392 if(PrepareEnvironment())
1394 m_strBuildTarget=strWhat;
1395 SetThermometerMax(250); // This is just a guess. The thread we are about to spawn will work out the correct answer
1397 UpdateThermometer(0);
1398 CloseHandle(CreateThread(NULL, 0, ThreadFunc, this, 0 ,&m_dwThreadId));
1400 strMsg.Format(_T("Building %s"),strWhat);
1401 SetIdleMessage(strMsg);
1403 SetTimer(42,1000,0); // This timer checks for process completion
1404 SetCurrentDirectory(pDoc->BuildTree());
1405 m_sp.Run(SubprocessOutputFunc, this, strCmd, false);
1412 if(pDoc->IsModified()||pDoc->BuildTree().IsEmpty()){
1413 SendMessage (WM_COMMAND, ID_FILE_SAVE);
1416 if(!(pDoc->IsModified()||pDoc->BuildTree().IsEmpty())){ // verify the save worked
1417 CString strCmd (_T("make"));
1418 if(!strWhat.IsEmpty()){
1419 strCmd+=_TCHAR(' ');
1422 if(!GetApp()->m_strMakeOptions.IsEmpty()){
1423 strCmd+=_TCHAR(' ');
1424 strCmd+=GetApp()->m_strMakeOptions;
1427 if(PrepareEnvironment()){
1428 m_strBuildTarget=strWhat;
1429 SetThermometerMax(250); // This is just a guess. The thread we are about to spawn will work out the correct answer
1431 UpdateThermometer(0);
1432 CloseHandle(CreateThread(NULL, 0, ThreadFunc, this, 0 ,&m_dwThreadId));
1434 strMsg.Format(_T("Building %s"),strWhat);
1435 SetIdleMessage(strMsg);
1437 SetTimer(42,1000,0); // This timer checks for process completion
1438 SetCurrentDirectory(pDoc->BuildTree());
1439 m_sp.Run(SubprocessOutputFunc, this, strCmd, false);
1446 void ecApp::OnProcessTerminated(wxProcess* process)
1448 m_pipedProcess = NULL;
1451 // ----------------------------------------------------------------------------
1453 // ----------------------------------------------------------------------------
1455 bool ecPipedProcess::HasInput()
1457 bool hasInput = FALSE;
1459 wxInputStream& is = *GetInputStream();
1462 wxTextInputStream tis(is);
1464 // this assumes that the output is always line buffered
1466 msg << tis.ReadLine();
1468 wxGetApp().Log(msg);
1473 wxInputStream& es = *GetErrorStream();
1476 wxTextInputStream tis(es);
1478 // this assumes that the output is always line buffered
1480 msg << tis.ReadLine();
1482 wxGetApp().Log(msg);
1490 void ecPipedProcess::OnTerminate(int pid, int status)
1492 // show the rest of the output
1493 while ( HasInput() )
1496 wxGetApp().OnProcessTerminated(this);
1498 //wxLogStatus(m_parent, _T("Process %u ('%s') terminated with exit code %d."),
1499 // pid, m_cmd.c_str(), status);
1501 // we're not needed any more
1505 void ecPingTimer::Notify()
1507 static bool s_inNotify = FALSE;
1514 // On Windows, simply having the timer going will ping the message queue
1515 // and cause idle processing to happen.
1516 // On Unix, this doesn't happen so we have to do the processing explicitly.
1521 if ( wxGetApp().m_pipedProcess )
1522 while (wxGetApp().m_pipedProcess->HasInput())
1524 // Loop while there is still input