]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - tools/src/tools/configtool/standalone/wxwin/ecutils.h
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / tools / src / tools / configtool / standalone / wxwin / ecutils.h
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 //===========================================================================
26 //#####DESCRIPTIONBEGIN####
27 //
28 // Author(s):   sdf
29 // Contact(s):  sdf
30 // Date:                1998/08/11
31 // Version:             0.01
32 // Purpose:     
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)
36 // Requires:    
37 // Provides:    
38 // See also:    
39 // Known bugs:  
40 // Usage:       
41 //
42 //####DESCRIPTIONEND####
43 //  
44 //===========================================================================
45
46 #ifndef _ECUTILS_H_
47 #define _ECUTILS_H_
48
49 #ifdef __GNUG__
50 #pragma interface "ecutils.cpp"
51 #endif
52
53 #include "wx/hash.h"
54 #include "wx/confbase.h"
55 #include "wx/dynarray.h"
56
57 #include "filename.h"
58 #include "cdl.hxx"
59
60 //#define INCLUDEFILE <string>
61 //#include "IncludeSTL.h"
62
63 //#include <stdarg.h>     // vsnprintf
64
65 /*
66  * ecUtils
67  * This class implements a miscellany of utility functions.
68  * They are being implemented as and when they are required during Configtool
69  * development.
70  */
71
72 class ecUtils {
73 public:
74 #if 0
75     static bool Launch (const ecFileName &strFileName,const ecFileName &strViewer);
76     static wxString GetLastErrorMessageString ();
77 #endif    
78
79     static bool AddToPath (const ecFileName &strFolder, bool bAtFront=true);
80
81 #if 0
82     static const wxString LoadString (UINT id);
83     
84     // Messagebox functions
85     
86     // Vararg message box compositor
87     static int MessageBoxF (LPCTSTR pszFormat, ...);
88     // Same, with type
89     static int MessageBoxFT (UINT nType, LPCTSTR pszFormat, ...);
90     // As above but with resource
91     static int MessageBoxFR (UINT nID, UINT nType, LPCTSTR pszFormat, ...);
92     // Same, with type
93     static int MessageBoxFR (UINT nID, LPCTSTR pszFormat, ...);
94     // vararg form
95     static int vMessageBox(UINT nType, LPCTSTR  pszFormat, va_list marker);
96     
97 #endif
98     // String functions
99     
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);
104
105     static int Chop(const wxString& psz,wxArrayString &ar, const wxString& pszSep,
106         bool bObserveStrings=FALSE, bool bBackslashQuotes = FALSE);
107
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);
112
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);
117
118     static void UnicodeToCStr(const wxChar* str,char *&psz);
119     static std::string UnicodeToStdStr(const wxChar* str);
120
121 #if 0    
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);
126 #endif
127 };
128
129 /*
130  * wxStringToStringMap
131  *
132  * Stores string values keyed by strings.
133  * Note that only one value is allowed per key.
134  */
135
136 class wxStringToStringMap: public wxObject
137 {
138 public:
139     wxStringToStringMap(): m_hashTable(wxKEY_STRING) {}
140     ~wxStringToStringMap() { Clear(); }
141
142 //// Operations
143
144     // Remove the key/value pair
145     bool Remove(const wxString& key);
146
147     // Clear the map
148     void Clear();
149
150     // Begin a search through the whole map: use Next to find key/value pairs
151     void BeginFind();
152
153     // Retrieve the next key/value pair, FALSE indicates end of data
154     bool Next(wxString& key, wxString& value);
155
156 //// Accessors
157
158     // Replaces the value (if any) associated with the key
159     void Set(const wxString& key, const wxString& value);
160
161     // Finds a value associated with the key, returns FALSE if none
162     bool Find(const wxString& key, wxString& value);
163
164 protected:
165     wxHashTable m_hashTable;
166 };
167
168 // Is str a member of arr?
169 bool wxArrayStringIsMember(const wxArrayString& arr, const wxString& str);
170
171 // Eliminate .. and .
172 wxString wxGetRealPath(const wxString& path);
173
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);
177
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);
184
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);
188
189 // Select the given item
190 void wxListCtrlSelectItem(wxListCtrl& listCtrl, long sel, bool deselectOther = TRUE);
191
192 // Find the selection
193 long wxListCtrlGetSelection(wxListCtrl& listCtrl);
194
195 // Find which column the cursor is on
196 int wxListCtrlFindColumn(wxListCtrl& listCtrl, int noCols, int x);
197
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);
201
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);
205
206 // Kill a process
207 int ecKill(long pid, wxSignal sig);
208
209 class WXDLLEXPORT wxOutputStream;
210
211 wxOutputStream& operator <<(wxOutputStream&, const wxString& s);
212 wxOutputStream& operator <<(wxOutputStream&, const char c);
213 wxOutputStream& operator <<(wxOutputStream&, long l);
214
215 /*
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
219  * the process.
220  */
221
222 class wxProcessKiller
223 {
224
225 public:
226     const wxString ErrorString() const;
227
228     wxProcessKiller(int pid);
229     ~wxProcessKiller();
230     
231     void SetVerbose   (bool b)         { m_bVerbose=b; }
232       
233     int Pid() const { return m_idProcess; } // returns process id (even when process is terminated)
234     
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; }
240     
241     // Kill the process:
242     bool Kill(bool bRecurse = true);
243     
244     // Is the process running?
245     bool ProcessAlive();
246     
247 protected:
248     static const wxString Name (int pid); // for debugging - only works under NT
249     
250     struct wxPInfo;
251     struct wxPInfo {
252         wxPInfo *pParent;
253 #ifdef _WIN32
254         wxLongLong_t tCreation;
255 #endif
256         long tCpu;
257         int PID;
258         int PPID;
259         bool IsChildOf(int pid) const;
260     };
261     
262     typedef std::vector<wxPInfo> wxPInfoArray;
263     
264     static bool PSExtract(wxPInfoArray &arPinfo);
265     static void SetParents(wxPInfoArray &arPinfo);
266     
267 #ifdef _WIN32
268     static long GetPlatform();
269     WXHANDLE m_hrPipe;
270     WXHANDLE m_hwPipe;
271     WXHANDLE m_hProcess;     // This handle is "owned" by the ThreadFunc
272     static WXHINSTANCE hInstLib1, hInstLib2;
273     int m_nErr;
274 #else
275     int m_tty;
276     wxString m_strCmd;
277 #endif
278     
279     bool m_bVerbose;
280     int m_nExitCode;
281     int m_idProcess;
282     
283     static const unsigned int PROCESS_KILL_EXIT_CODE;
284 };
285
286 /*
287  * ecDialog
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)
291  */
292
293 class ecDialog: public wxDialog
294 {
295 public:
296     DECLARE_CLASS(ecDialog)
297
298     ecDialog() {};
299
300     ecDialog(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos = wxDefaultPosition,
301         const wxSize& sz = wxDefaultSize, long style = wxDEFAULT_DIALOG_STYLE)
302     {
303         Create(parent, id, title, pos, sz, style);
304     }
305 /*
306     bool Create(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos = wxDefaultPosition,
307         const wxSize& sz = wxDefaultSize, long style = wxDEFAULT_DIALOG_STYLE);
308 */
309
310     void OnSize(wxSizeEvent& event);
311
312 DECLARE_EVENT_TABLE()
313 };
314
315 /*
316  * Implements saving/loading of window settings - fonts only for now
317  */
318
319 class wxWindowSettingsObject: public wxObject
320 {
321 public:
322     wxWindowSettingsObject(const wxString& name, wxWindow* win) { m_windowName = name; if (win) m_arrWindow.Add(win); }
323     wxString    m_windowName;
324     wxFont      m_font;
325     wxArrayPtrVoid m_arrWindow;
326 };
327
328 class wxWindowSettings: public wxObject
329 {
330 public:
331     wxWindowSettings() { m_useDefaults = TRUE; };
332     ~wxWindowSettings() { Clear(); }
333
334 //// Operations
335     wxWindowSettingsObject* FindSettings(const wxString& windowName) const;
336     void Add(const wxString& name, wxWindow* win = NULL) { m_settings.Append(new wxWindowSettingsObject(name, win)); }
337
338     bool LoadConfig(wxConfigBase& config);
339     bool SaveConfig(wxConfigBase& config);
340
341     bool LoadFont(wxConfigBase& config, const wxString& windowName, wxFont& font);
342     bool SaveFont(wxConfigBase& config, const wxString& windowName, const wxFont& font);
343
344     void Clear() { m_settings.DeleteContents(TRUE); m_settings.Clear(); m_settings.DeleteContents(FALSE); }
345
346     bool ApplyFontsToWindows();
347
348 //// Accessors
349     wxFont GetFont(const wxString& name) const;
350     void SetFont(const wxString& name, const wxFont& font);
351
352     wxWindow* GetWindow(const wxString& name) const;
353     void SetWindow(const wxString& name, wxWindow* win);
354
355     wxArrayPtrVoid* GetWindows(const wxString& name) const;
356     void SetWindows(const wxString& name, wxArrayPtrVoid& arr);
357
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; }
361
362     void SetUseDefaults(bool useDefaults) { m_useDefaults = useDefaults; }
363     bool GetUseDefaults() const { return m_useDefaults; }
364
365 public:
366     wxList  m_settings;
367     bool    m_useDefaults;
368 };
369
370 #endif
371 // _ECUTILS_H_