1 //####COPYRIGHTBEGIN####
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
6 // This program is part of the eCos host tools.
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)
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
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.
22 // ----------------------------------------------------------------------------
24 //####COPYRIGHTEND####
25 //===========================================================================
26 //#####DESCRIPTIONBEGIN####
33 // Description: Interface to various global utility functions. Everything in this
34 // class is static (there are no instances).
35 // Modified by julians for wxWindows (originally CTUtils.h)
42 //####DESCRIPTIONEND####
44 //===========================================================================
50 #pragma interface "ecutils.cpp"
54 #include "wx/confbase.h"
55 #include "wx/dynarray.h"
60 //#define INCLUDEFILE <string>
61 //#include "IncludeSTL.h"
63 //#include <stdarg.h> // vsnprintf
67 * This class implements a miscellany of utility functions.
68 * They are being implemented as and when they are required during Configtool
75 static bool Launch (const ecFileName &strFileName,const ecFileName &strViewer);
76 static wxString GetLastErrorMessageString ();
79 static bool AddToPath (const ecFileName &strFolder, bool bAtFront=true);
82 static const wxString LoadString (UINT id);
84 // Messagebox functions
86 // Vararg message box compositor
87 static int MessageBoxF (LPCTSTR pszFormat, ...);
89 static int MessageBoxFT (UINT nType, LPCTSTR pszFormat, ...);
90 // As above but with resource
91 static int MessageBoxFR (UINT nID, UINT nType, LPCTSTR pszFormat, ...);
93 static int MessageBoxFR (UINT nID, LPCTSTR pszFormat, ...);
95 static int vMessageBox(UINT nType, LPCTSTR pszFormat, va_list marker);
100 // Chop the string into pieces using separator cSep.
101 // The Boolean controls whether " and \ make a difference
102 static int Chop(const wxString& psz,wxArrayString &ar, wxChar cSep=wxT(' '),
103 bool bObserveStrings = FALSE, bool bBackslashQuotes = FALSE);
105 static int Chop(const wxString& psz,wxArrayString &ar, const wxString& pszSep,
106 bool bObserveStrings=FALSE, bool bBackslashQuotes = FALSE);
108 // String -> Integer, observing the current hex/decimal setting
109 //static BOOL StrToItemIntegerType(const wxString &str, __int64 &d);
110 static bool StrToItemIntegerType(const wxString &str, long &d);
111 static bool StrToDouble (const wxString &strValue, double &dValue);
113 // Integer -> String, observing the current hex/decimal setting
114 static const wxString IntToStr(long d,bool bHex=false);
115 static const wxString DoubleToStr (double dValue);
116 static wxString StripExtraWhitespace (const wxString & strInput);
118 static void UnicodeToCStr(const wxChar* str,char *&psz);
119 static std::string UnicodeToStdStr(const wxChar* str);
122 // Provide a failure explanation for what just went wrong
123 static const wxString Explanation (CFileException &exc);
124 static ecFileName WPath(const std::string &str);
125 static bool CopyFile (LPCTSTR pszSource,LPCTSTR pszDest);
130 * wxStringToStringMap
132 * Stores string values keyed by strings.
133 * Note that only one value is allowed per key.
136 class wxStringToStringMap: public wxObject
139 wxStringToStringMap(): m_hashTable(wxKEY_STRING) {}
140 ~wxStringToStringMap() { Clear(); }
144 // Remove the key/value pair
145 bool Remove(const wxString& key);
150 // Begin a search through the whole map: use Next to find key/value pairs
153 // Retrieve the next key/value pair, FALSE indicates end of data
154 bool Next(wxString& key, wxString& value);
158 // Replaces the value (if any) associated with the key
159 void Set(const wxString& key, const wxString& value);
161 // Finds a value associated with the key, returns FALSE if none
162 bool Find(const wxString& key, wxString& value);
165 wxHashTable m_hashTable;
168 // Is str a member of arr?
169 bool wxArrayStringIsMember(const wxArrayString& arr, const wxString& str);
171 // Eliminate .. and .
172 wxString wxGetRealPath(const wxString& path);
174 // A version of the above but prepending the cwd (current path) first
175 // if 'path' is relative
176 wxString wxGetRealPath(const wxString& cwd, const wxString& path);
178 // Find the absolute path where this application has been run from.
179 // argv0 is wxTheApp->argv[0]
180 // cwd is the current working directory (at startup)
181 // appVariableName is the name of a variable containing the directory for this app, e.g.
182 // MYAPPDIR. This is used as a last resort. Or should it be a first resort?
183 wxString wxFindAppPath(const wxString& argv0, const wxString& cwd, const wxString& appVariableName = wxEmptyString);
185 // Find the text of the list control item at the given column
186 class WXDLLEXPORT wxListCtrl;
187 wxString wxListCtrlGetItemTextColumn(wxListCtrl& listCtrl, long item, int col);
189 // Select the given item
190 void wxListCtrlSelectItem(wxListCtrl& listCtrl, long sel, bool deselectOther = TRUE);
192 // Find the selection
193 long wxListCtrlGetSelection(wxListCtrl& listCtrl);
195 // Find which column the cursor is on
196 int wxListCtrlFindColumn(wxListCtrl& listCtrl, int noCols, int x);
198 // Refresh children of this window (e.g. on Windows, laying out a window containing
199 // controls can cause screen corruption)
200 void wxRefreshControls(wxWindow* win);
202 // Make a path name with no separators, out of a full pathname,
203 // e.g. opt_ecos_ecos-1.4.5 out of /opt/ecos/ecos-1.4.5
204 wxString ecMakeNameFromPath(const wxString& path);
207 int ecKill(long pid, wxSignal sig);
209 class WXDLLEXPORT wxOutputStream;
211 wxOutputStream& operator <<(wxOutputStream&, const wxString& s);
212 wxOutputStream& operator <<(wxOutputStream&, const char c);
213 wxOutputStream& operator <<(wxOutputStream&, long l);
216 * Class for recursively killing a process and its children.
217 * This has been taken from CSubprocess, but we can't use CSubprocess
218 * directly for killing a process because it assumes it's been used to create
222 class wxProcessKiller
226 const wxString ErrorString() const;
228 wxProcessKiller(int pid);
231 void SetVerbose (bool b) { m_bVerbose=b; }
233 int Pid() const { return m_idProcess; } // returns process id (even when process is terminated)
235 // Get the process exit code. This can be:
236 // exit code of process (if terminated)
237 // 0xffffffff (if process not yet run)
238 // GetLastError result (if process could not be run)
239 int GetExitCode() { return m_nExitCode; }
242 bool Kill(bool bRecurse = true);
244 // Is the process running?
248 static const wxString Name (int pid); // for debugging - only works under NT
254 wxLongLong_t tCreation;
259 bool IsChildOf(int pid) const;
262 typedef std::vector<wxPInfo> wxPInfoArray;
264 static bool PSExtract(wxPInfoArray &arPinfo);
265 static void SetParents(wxPInfoArray &arPinfo);
268 static long GetPlatform();
271 WXHANDLE m_hProcess; // This handle is "owned" by the ThreadFunc
272 static WXHINSTANCE hInstLib1, hInstLib2;
283 static const unsigned int PROCESS_KILL_EXIT_CODE;
288 * Supports features we want to have for all dialogs in the application.
289 * So far, this just allows dialogs to be resizeable under MSW by
290 * refreshing the controls in OnSize (otherwise there's a mess)
293 class ecDialog: public wxDialog
296 DECLARE_CLASS(ecDialog)
300 ecDialog(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos = wxDefaultPosition,
301 const wxSize& sz = wxDefaultSize, long style = wxDEFAULT_DIALOG_STYLE)
303 Create(parent, id, title, pos, sz, style);
306 bool Create(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos = wxDefaultPosition,
307 const wxSize& sz = wxDefaultSize, long style = wxDEFAULT_DIALOG_STYLE);
310 void OnSize(wxSizeEvent& event);
312 DECLARE_EVENT_TABLE()
316 * Implements saving/loading of window settings - fonts only for now
319 class wxWindowSettingsObject: public wxObject
322 wxWindowSettingsObject(const wxString& name, wxWindow* win) { m_windowName = name; if (win) m_arrWindow.Add(win); }
323 wxString m_windowName;
325 wxArrayPtrVoid m_arrWindow;
328 class wxWindowSettings: public wxObject
331 wxWindowSettings() { m_useDefaults = TRUE; };
332 ~wxWindowSettings() { Clear(); }
335 wxWindowSettingsObject* FindSettings(const wxString& windowName) const;
336 void Add(const wxString& name, wxWindow* win = NULL) { m_settings.Append(new wxWindowSettingsObject(name, win)); }
338 bool LoadConfig(wxConfigBase& config);
339 bool SaveConfig(wxConfigBase& config);
341 bool LoadFont(wxConfigBase& config, const wxString& windowName, wxFont& font);
342 bool SaveFont(wxConfigBase& config, const wxString& windowName, const wxFont& font);
344 void Clear() { m_settings.DeleteContents(TRUE); m_settings.Clear(); m_settings.DeleteContents(FALSE); }
346 bool ApplyFontsToWindows();
349 wxFont GetFont(const wxString& name) const;
350 void SetFont(const wxString& name, const wxFont& font);
352 wxWindow* GetWindow(const wxString& name) const;
353 void SetWindow(const wxString& name, wxWindow* win);
355 wxArrayPtrVoid* GetWindows(const wxString& name) const;
356 void SetWindows(const wxString& name, wxArrayPtrVoid& arr);
358 int GetCount() const { return m_settings.Number(); }
359 wxWindowSettingsObject* GetNth(int i) const { return (wxWindowSettingsObject*) m_settings.Nth(i)->Data(); }
360 wxString GetName(int i) const { return GetNth(i)->m_windowName; }
362 void SetUseDefaults(bool useDefaults) { m_useDefaults = useDefaults; }
363 bool GetUseDefaults() const { return m_useDefaults; }