]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: utils.h | |
3 | // Purpose: Miscellaneous utilities | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Julian Smart | |
3f4a0c5b | 9 | // Licence: wxWindows license |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
34138703 JS |
12 | #ifndef _WX_UTILSH__ |
13 | #define _WX_UTILSH__ | |
c801d85f | 14 | |
d6b9496a VZ |
15 | // ---------------------------------------------------------------------------- |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
c801d85f | 19 | #ifdef __GNUG__ |
d6b9496a | 20 | #pragma interface "utils.h" |
c801d85f KB |
21 | #endif |
22 | ||
23 | #include "wx/setup.h" | |
24 | #include "wx/object.h" | |
25 | #include "wx/list.h" | |
26 | #include "wx/window.h" | |
27 | #include "wx/filefn.h" | |
28 | ||
3f4a0c5b | 29 | #include "wx/ioswrap.h" |
c801d85f KB |
30 | |
31 | #ifdef __X__ | |
d6b9496a VZ |
32 | #include <dirent.h> |
33 | #include <unistd.h> | |
c801d85f KB |
34 | #endif |
35 | ||
36 | #include <stdio.h> | |
37 | ||
d6b9496a VZ |
38 | // ---------------------------------------------------------------------------- |
39 | // Forward declaration | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | class WXDLLEXPORT wxProcess; | |
43 | class WXDLLEXPORT wxFrame; | |
44 | ||
45 | // FIXME should use wxStricmp() instead | |
c801d85f | 46 | #ifdef __GNUWIN32__ |
d6b9496a VZ |
47 | #define stricmp strcasecmp |
48 | #define strnicmp strncasecmp | |
c801d85f KB |
49 | #endif |
50 | ||
d6b9496a VZ |
51 | // ---------------------------------------------------------------------------- |
52 | // Macros | |
53 | // ---------------------------------------------------------------------------- | |
c801d85f | 54 | |
d6b9496a VZ |
55 | #define wxMax(a,b) (((a) > (b)) ? (a) : (b)) |
56 | #define wxMin(a,b) (((a) < (b)) ? (a) : (b)) | |
c801d85f | 57 | |
d6b9496a VZ |
58 | // ---------------------------------------------------------------------------- |
59 | // String functions (deprecated, use wxString) | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | // Useful buffer (FIXME VZ: yeah, that is. To be removed!) | |
9d2f3c71 | 63 | WXDLLEXPORT_DATA(extern wxChar*) wxBuffer; |
c801d85f KB |
64 | |
65 | // Make a copy of this string using 'new' | |
9d2f3c71 | 66 | WXDLLEXPORT wxChar* copystring(const wxChar *s); |
c801d85f | 67 | |
d6b9496a | 68 | // Matches string one within string two regardless of case |
9d2f3c71 | 69 | WXDLLEXPORT bool StringMatch(wxChar *one, wxChar *two, bool subString = TRUE, bool exact = FALSE); |
d6b9496a VZ |
70 | |
71 | // A shorter way of using strcmp | |
9d2f3c71 | 72 | #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0)) |
d6b9496a VZ |
73 | |
74 | // ---------------------------------------------------------------------------- | |
75 | // Miscellaneous functions | |
76 | // ---------------------------------------------------------------------------- | |
77 | ||
78 | // Sound the bell | |
79 | WXDLLEXPORT void wxBell(void) ; | |
80 | ||
81 | // Get OS version | |
82 | WXDLLEXPORT int wxGetOsVersion(int *majorVsn= (int *) NULL,int *minorVsn= (int *) NULL) ; | |
83 | ||
84 | // Return a string with the current date/time | |
85 | WXDLLEXPORT wxString wxNow(); | |
86 | ||
87 | // ---------------------------------------------------------------------------- | |
88 | // Window ID management | |
89 | // ---------------------------------------------------------------------------- | |
90 | ||
c801d85f | 91 | // Generate a unique ID |
afb74891 | 92 | WXDLLEXPORT long wxNewId(); |
d6b9496a VZ |
93 | #if !defined(NewId) && defined(WXWIN_COMPATIBILITY) |
94 | #define NewId wxNewId | |
95 | #endif | |
c801d85f KB |
96 | |
97 | // Ensure subsequent IDs don't clash with this one | |
184b5d99 | 98 | WXDLLEXPORT void wxRegisterId(long id); |
d6b9496a VZ |
99 | #if !defined(RegisterId) && defined(WXWIN_COMPATIBILITY) |
100 | #define RegisterId wxRegisterId | |
101 | #endif | |
c801d85f KB |
102 | |
103 | // Return the current ID | |
afb74891 | 104 | WXDLLEXPORT long wxGetCurrentId(); |
c801d85f | 105 | |
d6b9496a VZ |
106 | // ---------------------------------------------------------------------------- |
107 | // Various conversions | |
108 | // ---------------------------------------------------------------------------- | |
c801d85f | 109 | |
9d2f3c71 OK |
110 | WXDLLEXPORT_DATA(extern const wxChar*) wxFloatToStringStr; |
111 | WXDLLEXPORT_DATA(extern const wxChar*) wxDoubleToStringStr; | |
c801d85f | 112 | |
9d2f3c71 OK |
113 | WXDLLEXPORT void StringToFloat(wxChar *s, float *number); |
114 | WXDLLEXPORT wxChar* FloatToString(float number, const wxChar *fmt = wxFloatToStringStr); | |
115 | WXDLLEXPORT void StringToDouble(wxChar *s, double *number); | |
116 | WXDLLEXPORT wxChar* DoubleToString(double number, const wxChar *fmt = wxDoubleToStringStr); | |
117 | WXDLLEXPORT void StringToInt(wxChar *s, int *number); | |
118 | WXDLLEXPORT void StringToLong(wxChar *s, long *number); | |
119 | WXDLLEXPORT wxChar* IntToString(int number); | |
120 | WXDLLEXPORT wxChar* LongToString(long number); | |
c801d85f | 121 | |
c801d85f | 122 | // Convert 2-digit hex number to decimal |
184b5d99 | 123 | WXDLLEXPORT int wxHexToDec(const wxString& buf); |
c801d85f KB |
124 | |
125 | // Convert decimal integer to 2-character hex string | |
9d2f3c71 | 126 | WXDLLEXPORT void wxDecToHex(int dec, wxChar *buf); |
184b5d99 | 127 | WXDLLEXPORT wxString wxDecToHex(int dec); |
c801d85f | 128 | |
d6b9496a VZ |
129 | // ---------------------------------------------------------------------------- |
130 | // Process management | |
131 | // ---------------------------------------------------------------------------- | |
132 | ||
c801d85f | 133 | // Execute another program. Returns 0 if there was an error, a PID otherwise. |
9d2f3c71 | 134 | WXDLLEXPORT long wxExecute(wxChar **argv, bool sync = FALSE, |
c67daf87 | 135 | wxProcess *process = (wxProcess *) NULL); |
184b5d99 | 136 | WXDLLEXPORT long wxExecute(const wxString& command, bool sync = FALSE, |
c67daf87 | 137 | wxProcess *process = (wxProcess *) NULL); |
c801d85f | 138 | |
d6b9496a VZ |
139 | enum wxSignal |
140 | { | |
141 | wxSIGNONE = 0, // verify if the process exists under Unix | |
142 | wxSIGHUP, | |
143 | wxSIGINT, | |
144 | wxSIGQUIT, | |
145 | wxSIGILL, | |
146 | wxSIGTRAP, | |
147 | wxSIGABRT, | |
148 | wxSIGIOT = wxSIGABRT, // another name | |
149 | wxSIGEMT, | |
150 | wxSIGFPE, | |
151 | wxSIGKILL, | |
152 | wxSIGBUS, | |
153 | wxSIGSEGV, | |
154 | wxSIGSYS, | |
155 | wxSIGPIPE, | |
156 | wxSIGALRM, | |
157 | wxSIGTERM | |
158 | ||
159 | // further signals are different in meaning between different Unix systems | |
160 | }; | |
c801d85f | 161 | |
d6b9496a VZ |
162 | // the argument is ignored under Windows - the process is always killed |
163 | WXDLLEXPORT int wxKill(long pid, wxSignal sig = wxSIGTERM); | |
c801d85f KB |
164 | |
165 | // Execute a command in an interactive shell window | |
166 | // If no command then just the shell | |
62448488 | 167 | WXDLLEXPORT bool wxShell(const wxString& command = wxEmptyString); |
c801d85f KB |
168 | |
169 | // Sleep for nSecs seconds under UNIX, do nothing under Windows | |
184b5d99 | 170 | WXDLLEXPORT void wxSleep(int nSecs); |
c801d85f | 171 | |
afb74891 VZ |
172 | // Sleep for a given amount of milliseconds |
173 | WXDLLEXPORT void wxUsleep(unsigned long milliseconds); | |
174 | ||
c801d85f | 175 | // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) |
afb74891 | 176 | WXDLLEXPORT long wxGetFreeMemory(); |
c801d85f | 177 | |
d6b9496a VZ |
178 | // ---------------------------------------------------------------------------- |
179 | // Network and username functions. | |
180 | // ---------------------------------------------------------------------------- | |
c801d85f | 181 | |
d6b9496a | 182 | // NB: "char *" functions are deprecated, use wxString ones! |
c801d85f KB |
183 | |
184 | // Get eMail address | |
9d2f3c71 | 185 | WXDLLEXPORT bool wxGetEmailAddress(wxChar *buf, int maxSize); |
d6b9496a | 186 | WXDLLEXPORT wxString wxGetEmailAddress(); |
c801d85f KB |
187 | |
188 | // Get hostname. | |
9d2f3c71 | 189 | WXDLLEXPORT bool wxGetHostName(wxChar *buf, int maxSize); |
d6b9496a VZ |
190 | WXDLLEXPORT wxString wxGetHostName(); |
191 | ||
192 | // Get FQDN | |
193 | WXDLLEXPORT wxString wxGetFullHostName(); | |
c801d85f | 194 | |
d6b9496a | 195 | // Get user ID e.g. jacs (this is known as login name under Unix) |
9d2f3c71 | 196 | WXDLLEXPORT bool wxGetUserId(wxChar *buf, int maxSize); |
d6b9496a | 197 | WXDLLEXPORT wxString wxGetUserId(); |
c801d85f KB |
198 | |
199 | // Get user name e.g. Julian Smart | |
9d2f3c71 | 200 | WXDLLEXPORT bool wxGetUserName(wxChar *buf, int maxSize); |
d6b9496a VZ |
201 | WXDLLEXPORT wxString wxGetUserName(); |
202 | ||
203 | // Get current Home dir and copy to dest (returns pstr->c_str()) | |
9d2f3c71 | 204 | WXDLLEXPORT const wxChar* wxGetHomeDir(wxString *pstr); |
d6b9496a VZ |
205 | |
206 | // Get the user's home dir (caller must copy --- volatile) | |
207 | // returns NULL is no HOME dir is known | |
61ef57fc OK |
208 | #if defined(__UNIX__) && wxUSE_UNICODE |
209 | WXDLLEXPORT const wxMB2WXbuf wxGetUserHome(const wxString& user = wxEmptyString); | |
210 | #else | |
9d2f3c71 | 211 | WXDLLEXPORT wxChar* wxGetUserHome(const wxString& user = wxEmptyString); |
61ef57fc | 212 | #endif |
d6b9496a VZ |
213 | |
214 | // ---------------------------------------------------------------------------- | |
215 | // Strip out any menu codes | |
216 | // ---------------------------------------------------------------------------- | |
c801d85f | 217 | |
9d2f3c71 | 218 | WXDLLEXPORT wxChar* wxStripMenuCodes(wxChar *in, wxChar *out = (wxChar *) NULL); |
184b5d99 | 219 | WXDLLEXPORT wxString wxStripMenuCodes(const wxString& str); |
c801d85f | 220 | |
d6b9496a VZ |
221 | // ---------------------------------------------------------------------------- |
222 | // Window search | |
223 | // ---------------------------------------------------------------------------- | |
224 | ||
c801d85f KB |
225 | // Find the window/widget with the given title or label. |
226 | // Pass a parent to begin the search from, or NULL to look through | |
227 | // all windows. | |
184b5d99 | 228 | WXDLLEXPORT wxWindow* wxFindWindowByLabel(const wxString& title, wxWindow *parent = (wxWindow *) NULL); |
c801d85f KB |
229 | |
230 | // Find window by name, and if that fails, by label. | |
184b5d99 | 231 | WXDLLEXPORT wxWindow* wxFindWindowByName(const wxString& name, wxWindow *parent = (wxWindow *) NULL); |
c801d85f KB |
232 | |
233 | // Returns menu item id or -1 if none. | |
184b5d99 | 234 | WXDLLEXPORT int wxFindMenuItemId(wxFrame *frame, const wxString& menuString, const wxString& itemString); |
c801d85f | 235 | |
d6b9496a VZ |
236 | // ---------------------------------------------------------------------------- |
237 | // Message/event queue helpers | |
238 | // ---------------------------------------------------------------------------- | |
c801d85f KB |
239 | |
240 | // Yield to other apps/messages | |
afb74891 | 241 | WXDLLEXPORT bool wxYield(); |
c801d85f | 242 | |
ead7ce10 KB |
243 | // Yield to other apps/messages and disable user input |
244 | WXDLLEXPORT bool wxSafeYield(wxWindow *win = NULL); | |
245 | ||
95dee651 KB |
246 | // Enable or disable input to all top level windows |
247 | WXDLLEXPORT void wxEnableTopLevelWindows(bool enable = TRUE); | |
248 | ||
d6b9496a VZ |
249 | // Check whether this window wants to process messages, e.g. Stop button |
250 | // in long calculations. | |
251 | WXDLLEXPORT bool wxCheckForInterrupt(wxWindow *wnd); | |
252 | ||
253 | // Consume all events until no more left | |
254 | WXDLLEXPORT void wxFlushEvents(); | |
255 | ||
256 | // ---------------------------------------------------------------------------- | |
257 | // Cursors | |
258 | // ---------------------------------------------------------------------------- | |
c801d85f KB |
259 | |
260 | // Set the cursor to the busy cursor for all windows | |
261 | class WXDLLEXPORT wxCursor; | |
262 | WXDLLEXPORT_DATA(extern wxCursor*) wxHOURGLASS_CURSOR; | |
184b5d99 | 263 | WXDLLEXPORT void wxBeginBusyCursor(wxCursor *cursor = wxHOURGLASS_CURSOR); |
e2a6f233 | 264 | |
c801d85f | 265 | // Restore cursor to normal |
afb74891 | 266 | WXDLLEXPORT void wxEndBusyCursor(); |
d6b9496a | 267 | |
c801d85f | 268 | // TRUE if we're between the above two calls |
afb74891 | 269 | WXDLLEXPORT bool wxIsBusy(); |
c801d85f | 270 | |
e2a6f233 JS |
271 | // Convenience class so we can just create a wxBusyCursor object on the stack |
272 | class WXDLLEXPORT wxBusyCursor | |
273 | { | |
afb74891 VZ |
274 | public: |
275 | wxBusyCursor(wxCursor* cursor = wxHOURGLASS_CURSOR) | |
276 | { wxBeginBusyCursor(cursor); } | |
277 | ~wxBusyCursor() | |
278 | { wxEndBusyCursor(); } | |
e2a6f233 JS |
279 | }; |
280 | ||
d6b9496a VZ |
281 | // ---------------------------------------------------------------------------- |
282 | // Error message functions used by wxWindows (deprecated, use wxLog) | |
283 | // ---------------------------------------------------------------------------- | |
284 | ||
285 | // Format a message on the standard error (UNIX) or the debugging | |
286 | // stream (Windows) | |
9d2f3c71 | 287 | WXDLLEXPORT void wxDebugMsg(const wxChar *fmt ...) ; |
c801d85f KB |
288 | |
289 | // Non-fatal error (continues) | |
9d2f3c71 | 290 | WXDLLEXPORT_DATA(extern const wxChar*) wxInternalErrorStr; |
184b5d99 | 291 | WXDLLEXPORT void wxError(const wxString& msg, const wxString& title = wxInternalErrorStr); |
c801d85f KB |
292 | |
293 | // Fatal error (exits) | |
9d2f3c71 | 294 | WXDLLEXPORT_DATA(extern const wxChar*) wxFatalErrorStr; |
184b5d99 | 295 | WXDLLEXPORT void wxFatalError(const wxString& msg, const wxString& title = wxFatalErrorStr); |
c801d85f | 296 | |
d6b9496a | 297 | // ---------------------------------------------------------------------------- |
c801d85f | 298 | // Reading and writing resources (eg WIN.INI, .Xdefaults) |
d6b9496a VZ |
299 | // ---------------------------------------------------------------------------- |
300 | ||
47d67540 | 301 | #if wxUSE_RESOURCES |
62448488 JS |
302 | WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file = wxEmptyString); |
303 | WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file = wxEmptyString); | |
304 | WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file = wxEmptyString); | |
305 | WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file = wxEmptyString); | |
306 | ||
9d2f3c71 | 307 | WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file = wxEmptyString); |
62448488 JS |
308 | WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file = wxEmptyString); |
309 | WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file = wxEmptyString); | |
310 | WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file = wxEmptyString); | |
47d67540 | 311 | #endif // wxUSE_RESOURCES |
c801d85f | 312 | |
c801d85f KB |
313 | void WXDLLEXPORT wxGetMousePosition( int* x, int* y ); |
314 | ||
315 | // MSW only: get user-defined resource from the .res file. | |
316 | // Returns NULL or newly-allocated memory, so use delete[] to clean up. | |
2049ba38 | 317 | #ifdef __WXMSW__ |
9d2f3c71 OK |
318 | WXDLLEXPORT extern const wxChar* wxUserResourceStr; |
319 | WXDLLEXPORT wxChar* wxLoadUserResource(const wxString& resourceName, const wxString& resourceType = wxUserResourceStr); | |
c030b70f JS |
320 | |
321 | // Implemented in utils.cpp: VC++, Win95 only. Sets up a console for standard | |
322 | // input/output | |
323 | WXDLLEXPORT void wxRedirectIOToConsole(); | |
324 | ||
d6b9496a VZ |
325 | #endif // MSW |
326 | ||
327 | // ---------------------------------------------------------------------------- | |
328 | // Display and colorss (X only) | |
329 | // ---------------------------------------------------------------------------- | |
c801d85f | 330 | |
c801d85f | 331 | #ifdef __X__ |
d6b9496a VZ |
332 | WXDisplay *wxGetDisplay(); |
333 | bool wxSetDisplay(const wxString& display_name); | |
334 | wxString wxGetDisplayName(); | |
c801d85f KB |
335 | #endif |
336 | ||
337 | #ifdef __X__ | |
338 | ||
339 | #include <X11/Xlib.h> | |
340 | ||
341 | #define wxMAX_RGB 0xff | |
342 | #define wxMAX_SV 1000 | |
343 | #define wxSIGN(x) ((x < 0) ? -x : x) | |
344 | #define wxH_WEIGHT 4 | |
345 | #define wxS_WEIGHT 1 | |
346 | #define wxV_WEIGHT 2 | |
347 | ||
348 | typedef struct wx_hsv { | |
349 | int h,s,v; | |
350 | } wxHSV; | |
d6b9496a | 351 | |
c801d85f KB |
352 | #define wxMax3(x,y,z) ((x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z)) |
353 | #define wxMin3(x,y,z) ((x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z)) | |
354 | ||
c801d85f KB |
355 | void wxHSVToXColor(wxHSV *hsv,XColor *xcolor); |
356 | void wxXColorToHSV(wxHSV *hsv,XColor *xcolor); | |
357 | void wxAllocNearestColor(Display *display,Colormap colormap,XColor *xcolor); | |
358 | void wxAllocColor(Display *display,Colormap colormap,XColor *xcolor); | |
359 | ||
360 | #endif //__X__ | |
361 | ||
362 | #endif | |
34138703 | 363 | // _WX_UTILSH__ |