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