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####
26 //===========================================================================
27 //#####DESCRIPTIONBEGIN####
29 // Author(s): sdf, jld
34 // Description: Interface to various global utility functions. Everything in this
35 // class is static (there are no instances).
36 // Modified by julians for wxWindows (originally CTUtils.h)
43 //####DESCRIPTIONEND####
45 //===========================================================================
51 #pragma interface "ecutils.cpp"
55 #include "wx/confbase.h"
56 #include "wx/dynarray.h"
61 //#define INCLUDEFILE <string>
62 //#include "IncludeSTL.h"
64 //#include <stdarg.h> // vsnprintf
68 * This class implements a miscellany of utility functions.
69 * They are being implemented as and when they are required during Configtool
76 static bool Launch (const ecFileName &strFileName,const ecFileName &strViewer);
77 static wxString GetLastErrorMessageString ();
80 static const wxString PosixToNativePath(const wxString & posix);
81 static const wxString NativeToPosixPath(const wxString & native);
82 static bool AddToPath (const ecFileName &strFolder, bool bAtFront=true);
85 static const wxString LoadString (UINT id);
87 // Messagebox functions
89 // Vararg message box compositor
90 static int MessageBoxF (LPCTSTR pszFormat, ...);
92 static int MessageBoxFT (UINT nType, LPCTSTR pszFormat, ...);
93 // As above but with resource
94 static int MessageBoxFR (UINT nID, UINT nType, LPCTSTR pszFormat, ...);
96 static int MessageBoxFR (UINT nID, LPCTSTR pszFormat, ...);
98 static int vMessageBox(UINT nType, LPCTSTR pszFormat, va_list marker);
103 // Chop the string into pieces using separator cSep.
104 // The Boolean controls whether " and \ make a difference
105 static int Chop(const wxString& psz,wxArrayString &ar, wxChar cSep=wxT(' '),
106 bool bObserveStrings = FALSE, bool bBackslashQuotes = FALSE);
108 static int Chop(const wxString& psz,wxArrayString &ar, const wxString& pszSep,
109 bool bObserveStrings=FALSE, bool bBackslashQuotes = FALSE);
111 // String -> Integer, observing the current hex/decimal setting
112 //static BOOL StrToItemIntegerType(const wxString &str, __int64 &d);
113 static bool StrToItemIntegerType(const wxString &str, long &d);
114 static bool StrToDouble (const wxString &strValue, double &dValue);
116 // Integer -> String, observing the current hex/decimal setting
117 static const wxString IntToStr(long d,bool bHex=false);
118 static const wxString DoubleToStr (double dValue);
119 static wxString StripExtraWhitespace (const wxString & strInput);
121 static void UnicodeToCStr(const wxChar* str,char *&psz);
122 static std::string UnicodeToStdStr(const wxChar* str);
125 // Provide a failure explanation for what just went wrong
126 static const wxString Explanation (CFileException &exc);
127 static ecFileName WPath(const std::string &str);
128 static bool CopyFile (LPCTSTR pszSource,LPCTSTR pszDest);
133 * wxStringToStringMap
135 * Stores string values keyed by strings.
136 * Note that only one value is allowed per key.
139 class wxStringToStringMap: public wxObject
142 wxStringToStringMap(): m_hashTable(wxKEY_STRING) {}
143 ~wxStringToStringMap() { Clear(); }
147 // Remove the key/value pair
148 bool Remove(const wxString& key);
153 // Begin a search through the whole map: use Next to find key/value pairs
156 // Retrieve the next key/value pair, FALSE indicates end of data
157 bool Next(wxString& key, wxString& value);
161 // Replaces the value (if any) associated with the key
162 void Set(const wxString& key, const wxString& value);
164 // Finds a value associated with the key, returns FALSE if none
165 bool Find(const wxString& key, wxString& value);
168 wxHashTable m_hashTable;
171 // Is str a member of arr?
172 bool wxArrayStringIsMember(const wxArrayString& arr, const wxString& str);
174 // Eliminate .. and .
175 wxString wxGetRealPath(const wxString& path);
177 // A version of the above but prepending the cwd (current path) first
178 // if 'path' is relative
179 wxString wxGetRealPath(const wxString& cwd, const wxString& path);
181 // Find the absolute path where this application has been run from.
182 // argv0 is wxTheApp->argv[0]
183 // cwd is the current working directory (at startup)
184 // appVariableName is the name of a variable containing the directory for this app, e.g.
185 // MYAPPDIR. This is used as a last resort. Or should it be a first resort?
186 wxString wxFindAppPath(const wxString& argv0, const wxString& cwd, const wxString& appVariableName = wxEmptyString);
188 // Find the text of the list control item at the given column
189 class WXDLLEXPORT wxListCtrl;
190 wxString wxListCtrlGetItemTextColumn(wxListCtrl& listCtrl, long item, int col);
192 // Select the given item
193 void wxListCtrlSelectItem(wxListCtrl& listCtrl, long sel, bool deselectOther = TRUE);
195 // Find the selection
196 long wxListCtrlGetSelection(wxListCtrl& listCtrl);
198 // Find which column the cursor is on
199 int wxListCtrlFindColumn(wxListCtrl& listCtrl, int noCols, int x);
201 // Refresh children of this window (e.g. on Windows, laying out a window containing
202 // controls can cause screen corruption)
203 void wxRefreshControls(wxWindow* win);
205 // Make a path name with no separators, out of a full pathname,
206 // e.g. opt_ecos_ecos-1.4.5 out of /opt/ecos/ecos-1.4.5
207 wxString ecMakeNameFromPath(const wxString& path);
210 int ecKill(long pid, wxSignal sig);
212 class WXDLLEXPORT wxOutputStream;
214 wxOutputStream& operator <<(wxOutputStream&, const wxString& s);
215 wxOutputStream& operator <<(wxOutputStream&, const char c);
216 wxOutputStream& operator <<(wxOutputStream&, long l);
219 * Class for recursively killing a process and its children.
220 * This has been taken from CSubprocess, but we can't use CSubprocess
221 * directly for killing a process because it assumes it's been used to create
225 class wxProcessKiller
229 const wxString ErrorString() const;
231 wxProcessKiller(int pid);
234 void SetVerbose (bool b) { m_bVerbose=b; }
236 int Pid() const { return m_idProcess; } // returns process id (even when process is terminated)
238 // Get the process exit code. This can be:
239 // exit code of process (if terminated)
240 // 0xffffffff (if process not yet run)
241 // GetLastError result (if process could not be run)
242 int GetExitCode() { return m_nExitCode; }
245 bool Kill(bool bRecurse = true);
247 // Is the process running?
251 static const wxString Name (int pid); // for debugging - only works under NT
257 wxLongLong_t tCreation;
262 bool IsChildOf(int pid) const;
265 typedef std::vector<wxPInfo> wxPInfoArray;
267 static bool PSExtract(wxPInfoArray &arPinfo);
268 static void SetParents(wxPInfoArray &arPinfo);
271 static long GetPlatform();
274 WXHANDLE m_hProcess; // This handle is "owned" by the ThreadFunc
275 static WXHINSTANCE hInstLib1, hInstLib2;
286 static const unsigned int PROCESS_KILL_EXIT_CODE;
291 * Supports features we want to have for all dialogs in the application.
292 * So far, this just allows dialogs to be resizeable under MSW by
293 * refreshing the controls in OnSize (otherwise there's a mess)
296 class ecDialog: public wxDialog
299 DECLARE_CLASS(ecDialog)
303 ecDialog(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos = wxDefaultPosition,
304 const wxSize& sz = wxDefaultSize, long style = wxDEFAULT_DIALOG_STYLE)
306 Create(parent, id, title, pos, sz, style);
309 bool Create(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos = wxDefaultPosition,
310 const wxSize& sz = wxDefaultSize, long style = wxDEFAULT_DIALOG_STYLE);
313 void OnSize(wxSizeEvent& event);
315 DECLARE_EVENT_TABLE()
319 * Implements saving/loading of window settings - fonts only for now
322 class wxWindowSettingsObject: public wxObject
325 wxWindowSettingsObject(const wxString& name, wxWindow* win) { m_windowName = name; if (win) m_arrWindow.Add(win); }
326 wxString m_windowName;
328 wxArrayPtrVoid m_arrWindow;
331 class wxWindowSettings: public wxObject
334 wxWindowSettings() { m_useDefaults = TRUE; };
335 ~wxWindowSettings() { Clear(); }
338 wxWindowSettingsObject* FindSettings(const wxString& windowName) const;
339 void Add(const wxString& name, wxWindow* win = NULL) { m_settings.Append(new wxWindowSettingsObject(name, win)); }
341 bool LoadConfig(wxConfigBase& config);
342 bool SaveConfig(wxConfigBase& config);
344 bool LoadFont(wxConfigBase& config, const wxString& windowName, wxFont& font);
345 bool SaveFont(wxConfigBase& config, const wxString& windowName, const wxFont& font);
347 void Clear() { m_settings.DeleteContents(TRUE); m_settings.Clear(); m_settings.DeleteContents(FALSE); }
349 bool ApplyFontsToWindows();
352 wxFont GetFont(const wxString& name) const;
353 void SetFont(const wxString& name, const wxFont& font);
355 wxWindow* GetWindow(const wxString& name) const;
356 void SetWindow(const wxString& name, wxWindow* win);
358 wxArrayPtrVoid* GetWindows(const wxString& name) const;
359 void SetWindows(const wxString& name, wxArrayPtrVoid& arr);
361 int GetCount() const { return m_settings.Number(); }
362 wxWindowSettingsObject* GetNth(int i) const { return (wxWindowSettingsObject*) m_settings.Nth(i)->Data(); }
363 wxString GetName(int i) const { return GetNth(i)->m_windowName; }
365 void SetUseDefaults(bool useDefaults) { m_useDefaults = useDefaults; }
366 bool GetUseDefaults() const { return m_useDefaults; }