]> git.saurik.com Git - wxWidgets.git/blob - include/wx/utils.h
Forgot to commit
[wxWidgets.git] / include / wx / utils.h
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
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_UTILSH__
13 #define _WX_UTILSH__
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #ifdef __GNUG__
20 #pragma interface "utils.h"
21 #endif
22
23 #include "wx/setup.h"
24 #include "wx/object.h"
25 #include "wx/list.h"
26 #include "wx/filefn.h"
27
28 // need this for wxGetDiskSpace() as we can't, unfortunately, forward declare
29 // wxLongLong
30 #include "wx/longlong.h"
31
32 #ifdef __X__
33 #include <dirent.h>
34 #include <unistd.h>
35 #endif
36
37 #include <stdio.h>
38
39 // ----------------------------------------------------------------------------
40 // Forward declaration
41 // ----------------------------------------------------------------------------
42
43 class WXDLLEXPORT wxProcess;
44 class WXDLLEXPORT wxFrame;
45 class WXDLLEXPORT wxWindow;
46 class WXDLLEXPORT wxWindowList;
47 class WXDLLEXPORT wxPoint;
48
49 // ----------------------------------------------------------------------------
50 // Macros
51 // ----------------------------------------------------------------------------
52
53 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
54 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
55
56 // ----------------------------------------------------------------------------
57 // String functions (deprecated, use wxString)
58 // ----------------------------------------------------------------------------
59
60 // Useful buffer (FIXME VZ: To be removed!!!)
61 WXDLLEXPORT_DATA(extern wxChar*) wxBuffer;
62
63 // Make a copy of this string using 'new'
64 WXDLLEXPORT wxChar* copystring(const wxChar *s);
65
66 // Matches string one within string two regardless of case
67 WXDLLEXPORT bool StringMatch(wxChar *one, wxChar *two, bool subString = TRUE, bool exact = FALSE);
68
69 // A shorter way of using strcmp
70 #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
71
72 // ----------------------------------------------------------------------------
73 // Miscellaneous functions
74 // ----------------------------------------------------------------------------
75
76 // Sound the bell
77 WXDLLEXPORT void wxBell();
78
79 // Get OS description as a user-readable string
80 WXDLLEXPORT wxString wxGetOsDescription();
81
82 // Get OS version
83 WXDLLEXPORT int wxGetOsVersion(int *majorVsn = (int *) NULL,
84 int *minorVsn = (int *) NULL);
85
86 // Return a string with the current date/time
87 WXDLLEXPORT wxString wxNow();
88
89 // Return path where wxWindows is installed (mostly useful in Unices)
90 WXDLLEXPORT const wxChar *wxGetInstallPrefix();
91
92
93 #if wxUSE_GUI
94 // Don't synthesize KeyUp events holding down a key and producing
95 // KeyDown events with autorepeat. On by default and always on
96 // in wxMSW.
97 WXDLLEXPORT bool wxSetDetectableAutoRepeat( bool flag );
98
99 // ----------------------------------------------------------------------------
100 // Window ID management
101 // ----------------------------------------------------------------------------
102
103 // Generate a unique ID
104 WXDLLEXPORT long wxNewId();
105 #if !defined(NewId) && defined(WXWIN_COMPATIBILITY)
106 #define NewId wxNewId
107 #endif
108
109 // Ensure subsequent IDs don't clash with this one
110 WXDLLEXPORT void wxRegisterId(long id);
111 #if !defined(RegisterId) && defined(WXWIN_COMPATIBILITY)
112 #define RegisterId wxRegisterId
113 #endif
114
115 // Return the current ID
116 WXDLLEXPORT long wxGetCurrentId();
117
118 #endif // wxUSE_GUI
119
120 // ----------------------------------------------------------------------------
121 // Various conversions
122 // ----------------------------------------------------------------------------
123
124 WXDLLEXPORT_DATA(extern const wxChar*) wxFloatToStringStr;
125 WXDLLEXPORT_DATA(extern const wxChar*) wxDoubleToStringStr;
126
127 WXDLLEXPORT void StringToFloat(wxChar *s, float *number);
128 WXDLLEXPORT wxChar* FloatToString(float number, const wxChar *fmt = wxFloatToStringStr);
129 WXDLLEXPORT void StringToDouble(wxChar *s, double *number);
130 WXDLLEXPORT wxChar* DoubleToString(double number, const wxChar *fmt = wxDoubleToStringStr);
131 WXDLLEXPORT void StringToInt(wxChar *s, int *number);
132 WXDLLEXPORT void StringToLong(wxChar *s, long *number);
133 WXDLLEXPORT wxChar* IntToString(int number);
134 WXDLLEXPORT wxChar* LongToString(long number);
135
136 // Convert 2-digit hex number to decimal
137 WXDLLEXPORT int wxHexToDec(const wxString& buf);
138
139 // Convert decimal integer to 2-character hex string
140 WXDLLEXPORT void wxDecToHex(int dec, wxChar *buf);
141 WXDLLEXPORT wxString wxDecToHex(int dec);
142
143 // ----------------------------------------------------------------------------
144 // Process management
145 // ----------------------------------------------------------------------------
146
147 // Execute another program. Returns 0 if there was an error, a PID otherwise.
148 WXDLLEXPORT long wxExecute(wxChar **argv, bool sync = FALSE,
149 wxProcess *process = (wxProcess *) NULL);
150 WXDLLEXPORT long wxExecute(const wxString& command, bool sync = FALSE,
151 wxProcess *process = (wxProcess *) NULL);
152
153 // execute the command capturing its output into an array line by line
154 WXDLLEXPORT long wxExecute(const wxString& command,
155 wxArrayString& output);
156
157 // also capture stderr
158 WXDLLEXPORT long wxExecute(const wxString& command,
159 wxArrayString& output,
160 wxArrayString& error);
161
162 enum wxSignal
163 {
164 wxSIGNONE = 0, // verify if the process exists under Unix
165 wxSIGHUP,
166 wxSIGINT,
167 wxSIGQUIT,
168 wxSIGILL,
169 wxSIGTRAP,
170 wxSIGABRT,
171 wxSIGIOT = wxSIGABRT, // another name
172 wxSIGEMT,
173 wxSIGFPE,
174 wxSIGKILL,
175 wxSIGBUS,
176 wxSIGSEGV,
177 wxSIGSYS,
178 wxSIGPIPE,
179 wxSIGALRM,
180 wxSIGTERM
181
182 // further signals are different in meaning between different Unix systems
183 };
184
185 enum wxKillError
186 {
187 wxKILL_OK, // no error
188 wxKILL_BAD_SIGNAL, // no such signal
189 wxKILL_ACCESS_DENIED, // permission denied
190 wxKILL_NO_PROCESS, // no such process
191 wxKILL_ERROR // another, unspecified error
192 };
193
194 // send the given signal to the process (only NONE and KILL are supported under
195 // Windows, all others mean TERM), return 0 if ok and -1 on error
196 //
197 // return detailed error in rc if not NULL
198 WXDLLEXPORT int wxKill(long pid,
199 wxSignal sig = wxSIGTERM,
200 wxKillError *rc = NULL);
201
202 // Execute a command in an interactive shell window (always synchronously)
203 // If no command then just the shell
204 WXDLLEXPORT bool wxShell(const wxString& command = wxEmptyString);
205
206 // As wxShell(), but must give a (non interactive) command and its output will
207 // be returned in output array
208 WXDLLEXPORT bool wxShell(const wxString& command, wxArrayString& output);
209
210 // Sleep for nSecs seconds
211 WXDLLEXPORT void wxSleep(int nSecs);
212
213 // Sleep for a given amount of milliseconds
214 WXDLLEXPORT void wxUsleep(unsigned long milliseconds);
215
216 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
217 WXDLLEXPORT long wxGetFreeMemory();
218
219 // should wxApp::OnFatalException() be called?
220 WXDLLEXPORT bool wxHandleFatalExceptions(bool doit = TRUE);
221
222 // ----------------------------------------------------------------------------
223 // Environment variables
224 // ----------------------------------------------------------------------------
225
226 // returns TRUE if variable exists (value may be NULL if you just want to check
227 // for this)
228 WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value);
229
230 // set the env var name to the given value, return TRUE on success
231 WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value);
232
233 // remove the env var from environment
234 inline bool wxUnsetEnv(const wxString& var) { return wxSetEnv(var, NULL); }
235
236 // ----------------------------------------------------------------------------
237 // Network and username functions.
238 // ----------------------------------------------------------------------------
239
240 // NB: "char *" functions are deprecated, use wxString ones!
241
242 // Get eMail address
243 WXDLLEXPORT bool wxGetEmailAddress(wxChar *buf, int maxSize);
244 WXDLLEXPORT wxString wxGetEmailAddress();
245
246 // Get hostname.
247 WXDLLEXPORT bool wxGetHostName(wxChar *buf, int maxSize);
248 WXDLLEXPORT wxString wxGetHostName();
249
250 // Get FQDN
251 WXDLLEXPORT wxString wxGetFullHostName();
252 WXDLLEXPORT bool wxGetFullHostName(wxChar *buf, int maxSize);
253
254 // Get user ID e.g. jacs (this is known as login name under Unix)
255 WXDLLEXPORT bool wxGetUserId(wxChar *buf, int maxSize);
256 WXDLLEXPORT wxString wxGetUserId();
257
258 // Get user name e.g. Julian Smart
259 WXDLLEXPORT bool wxGetUserName(wxChar *buf, int maxSize);
260 WXDLLEXPORT wxString wxGetUserName();
261
262 // Get current Home dir and copy to dest (returns pstr->c_str())
263 WXDLLEXPORT wxString wxGetHomeDir();
264 WXDLLEXPORT const wxChar* wxGetHomeDir(wxString *pstr);
265
266 // Get the user's home dir (caller must copy --- volatile)
267 // returns NULL is no HOME dir is known
268 #if defined(__UNIX__) && wxUSE_UNICODE
269 WXDLLEXPORT const wxMB2WXbuf wxGetUserHome(const wxString& user = wxEmptyString);
270 #else
271 WXDLLEXPORT wxChar* wxGetUserHome(const wxString& user = wxEmptyString);
272 #endif
273
274 #ifdef __WXMAC__
275 WXDLLEXPORT wxString wxMacFindFolder(short vRefNum,
276 OSType folderType,
277 Boolean createFolder);
278 #endif
279
280 // get number of total/free bytes on the disk where path belongs
281 WXDLLEXPORT bool wxGetDiskSpace(const wxString& path,
282 wxLongLong *pTotal = NULL,
283 wxLongLong *pFree = NULL);
284
285 #if wxUSE_GUI // GUI only things from now on
286
287 // ----------------------------------------------------------------------------
288 // Menu accelerators related things
289 // ----------------------------------------------------------------------------
290
291 WXDLLEXPORT wxChar* wxStripMenuCodes(wxChar *in, wxChar *out = (wxChar *) NULL);
292 WXDLLEXPORT wxString wxStripMenuCodes(const wxString& str);
293
294 #if wxUSE_ACCEL
295 class WXDLLEXPORT wxAcceleratorEntry;
296 WXDLLEXPORT wxAcceleratorEntry *wxGetAccelFromString(const wxString& label);
297 #endif // wxUSE_ACCEL
298
299 // ----------------------------------------------------------------------------
300 // Window search
301 // ----------------------------------------------------------------------------
302
303 // Find the window/widget with the given title or label.
304 // Pass a parent to begin the search from, or NULL to look through
305 // all windows.
306 WXDLLEXPORT wxWindow* wxFindWindowByLabel(const wxString& title, wxWindow *parent = (wxWindow *) NULL);
307
308 // Find window by name, and if that fails, by label.
309 WXDLLEXPORT wxWindow* wxFindWindowByName(const wxString& name, wxWindow *parent = (wxWindow *) NULL);
310
311 // Returns menu item id or -1 if none.
312 WXDLLEXPORT int wxFindMenuItemId(wxFrame *frame, const wxString& menuString, const wxString& itemString);
313
314 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
315 // is always present but may be less reliable than a native version.
316 WXDLLEXPORT wxWindow* wxGenericFindWindowAtPoint(const wxPoint& pt);
317 WXDLLEXPORT wxWindow* wxFindWindowAtPoint(const wxPoint& pt);
318
319 // ----------------------------------------------------------------------------
320 // Message/event queue helpers
321 // ----------------------------------------------------------------------------
322
323 // Yield to other apps/messages
324 WXDLLEXPORT bool wxYield();
325
326 // Like wxYield, but fails silently if the yield is recursive.
327 WXDLLEXPORT bool wxYieldIfNeeded();
328
329 // Yield to other apps/messages and disable user input
330 WXDLLEXPORT bool wxSafeYield(wxWindow *win = NULL);
331
332 // Enable or disable input to all top level windows
333 WXDLLEXPORT void wxEnableTopLevelWindows(bool enable = TRUE);
334
335 // Check whether this window wants to process messages, e.g. Stop button
336 // in long calculations.
337 WXDLLEXPORT bool wxCheckForInterrupt(wxWindow *wnd);
338
339 // Consume all events until no more left
340 WXDLLEXPORT void wxFlushEvents();
341
342 // a class which disables all windows (except, may be, thegiven one) in its
343 // ctor and enables them back in its dtor
344 class WXDLLEXPORT wxWindowDisabler
345 {
346 public:
347 wxWindowDisabler(wxWindow *winToSkip = (wxWindow *)NULL);
348 ~wxWindowDisabler();
349
350 private:
351 wxWindowList *m_winDisabled;
352 };
353
354 // ----------------------------------------------------------------------------
355 // Cursors
356 // ----------------------------------------------------------------------------
357
358 // Set the cursor to the busy cursor for all windows
359 class WXDLLEXPORT wxCursor;
360 WXDLLEXPORT_DATA(extern wxCursor*) wxHOURGLASS_CURSOR;
361 WXDLLEXPORT void wxBeginBusyCursor(wxCursor *cursor = wxHOURGLASS_CURSOR);
362
363 // Restore cursor to normal
364 WXDLLEXPORT void wxEndBusyCursor();
365
366 // TRUE if we're between the above two calls
367 WXDLLEXPORT bool wxIsBusy();
368
369 // Convenience class so we can just create a wxBusyCursor object on the stack
370 class WXDLLEXPORT wxBusyCursor
371 {
372 public:
373 wxBusyCursor(wxCursor* cursor = wxHOURGLASS_CURSOR)
374 { wxBeginBusyCursor(cursor); }
375 ~wxBusyCursor()
376 { wxEndBusyCursor(); }
377
378 // FIXME: These two methods are currently only implemented (and needed?)
379 // in wxGTK. BusyCursor handling should probably be moved to
380 // common code since the wxGTK and wxMSW implementations are very
381 // similar except for wxMSW using HCURSOR directly instead of
382 // wxCursor.. -- RL.
383 static const wxCursor &GetStoredCursor();
384 static const wxCursor GetBusyCursor();
385 };
386
387
388 // ----------------------------------------------------------------------------
389 // Reading and writing resources (eg WIN.INI, .Xdefaults)
390 // ----------------------------------------------------------------------------
391
392 #if wxUSE_RESOURCES
393 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file = wxEmptyString);
394 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file = wxEmptyString);
395 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file = wxEmptyString);
396 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file = wxEmptyString);
397
398 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file = wxEmptyString);
399 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file = wxEmptyString);
400 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file = wxEmptyString);
401 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file = wxEmptyString);
402 #endif // wxUSE_RESOURCES
403
404 void WXDLLEXPORT wxGetMousePosition( int* x, int* y );
405
406 // MSW only: get user-defined resource from the .res file.
407 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
408 #ifdef __WXMSW__
409 WXDLLEXPORT extern const wxChar* wxUserResourceStr;
410 WXDLLEXPORT wxChar* wxLoadUserResource(const wxString& resourceName, const wxString& resourceType = wxUserResourceStr);
411
412 // Implemented in utils.cpp: VC++, Win95 only. Sets up a console for standard
413 // input/output
414 WXDLLEXPORT void wxRedirectIOToConsole();
415
416 #endif // MSW
417
418 // ----------------------------------------------------------------------------
419 // Display and colorss (X only)
420 // ----------------------------------------------------------------------------
421
422 #ifdef __WXGTK__
423 void *wxGetDisplay();
424 #endif
425
426 #ifdef __X__
427 WXDisplay *wxGetDisplay();
428 bool wxSetDisplay(const wxString& display_name);
429 wxString wxGetDisplayName();
430 #endif // X or GTK+
431
432 #ifdef __X__
433
434 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
435 // The resulting warnings are switched off here
436 #pragma message disable nosimpint
437 #endif
438 #include <X11/Xlib.h>
439 #ifdef __VMS__
440 #pragma message enable nosimpint
441 #endif
442
443 #define wxMAX_RGB 0xff
444 #define wxMAX_SV 1000
445 #define wxSIGN(x) ((x < 0) ? -x : x)
446 #define wxH_WEIGHT 4
447 #define wxS_WEIGHT 1
448 #define wxV_WEIGHT 2
449
450 typedef struct wx_hsv {
451 int h,s,v;
452 } wxHSV;
453
454 #define wxMax3(x,y,z) ((x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z))
455 #define wxMin3(x,y,z) ((x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z))
456
457 void wxHSVToXColor(wxHSV *hsv,XColor *xcolor);
458 void wxXColorToHSV(wxHSV *hsv,XColor *xcolor);
459 void wxAllocNearestColor(Display *display,Colormap colormap,XColor *xcolor);
460 void wxAllocColor(Display *display,Colormap colormap,XColor *xcolor);
461
462 #endif //__X__
463
464 #endif // wxUSE_GUI
465
466 // ----------------------------------------------------------------------------
467 // Error message functions used by wxWindows (deprecated, use wxLog)
468 // ----------------------------------------------------------------------------
469
470 // Format a message on the standard error (UNIX) or the debugging
471 // stream (Windows)
472 WXDLLEXPORT void wxDebugMsg(const wxChar *fmt ...);
473
474 // Non-fatal error (continues)
475 WXDLLEXPORT_DATA(extern const wxChar*) wxInternalErrorStr;
476 WXDLLEXPORT void wxError(const wxString& msg, const wxString& title = wxInternalErrorStr);
477
478 // Fatal error (exits)
479 WXDLLEXPORT_DATA(extern const wxChar*) wxFatalErrorStr;
480 WXDLLEXPORT void wxFatalError(const wxString& msg, const wxString& title = wxFatalErrorStr);
481
482
483 #endif
484 // _WX_UTILSH__