1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Miscellaneous utilities
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) && !defined(__EMX__)
20 // Some older compilers (such as EMX) cannot handle
21 // #pragma interface/implementation correctly, iff
22 // #pragma implementation is used in _two_ translation
23 // units (as created by e.g. event.cpp compiled for
24 // libwx_base and event.cpp compiled for libwx_gui_core).
25 // So we must not use those pragmas for those compilers in
27 #pragma interface "utils.h"
30 #include "wx/object.h"
32 #include "wx/filefn.h"
34 class WXDLLIMPEXP_BASE wxArrayString
;
36 // need this for wxGetDiskSpace() as we can't, unfortunately, forward declare
38 #include "wx/longlong.h"
47 // ----------------------------------------------------------------------------
48 // Forward declaration
49 // ----------------------------------------------------------------------------
51 class WXDLLIMPEXP_CORE wxProcess
;
52 class WXDLLIMPEXP_CORE wxFrame
;
53 class WXDLLIMPEXP_CORE wxWindow
;
54 class WXDLLIMPEXP_CORE wxWindowList
;
55 class WXDLLIMPEXP_CORE wxPoint
;
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
62 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
64 // wxGetFreeMemory can return huge amount of memory on 64-bit platforms
65 // define wxMemorySize according to the type which best fits your platform
66 #if wxUSE_LONGLONG && defined(__WIN64__)
67 // 64 bit Windowses have sizeof(long) only 32 bit long
68 // we need to use wxLongLong to express memory sizes
69 #define wxMemorySize wxLongLong
71 // 64 bit UNIX has sizeof(long) = 64
72 // assume 32 bit platforms cannnot return more than 32bits of
73 #define wxMemorySize long
76 // ----------------------------------------------------------------------------
77 // String functions (deprecated, use wxString)
78 // ----------------------------------------------------------------------------
80 // Make a copy of this string using 'new'
81 #if WXWIN_COMPATIBILITY_2_4
82 WXDLLIMPEXP_BASE wxChar
* copystring(const wxChar
*s
);
85 // A shorter way of using strcmp
86 #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
88 // ----------------------------------------------------------------------------
89 // Miscellaneous functions
90 // ----------------------------------------------------------------------------
93 WXDLLIMPEXP_BASE
void wxBell();
95 // Get OS description as a user-readable string
96 WXDLLIMPEXP_BASE wxString
wxGetOsDescription();
99 WXDLLIMPEXP_BASE
int wxGetOsVersion(int *majorVsn
= (int *) NULL
,
100 int *minorVsn
= (int *) NULL
);
102 // Return a string with the current date/time
103 WXDLLIMPEXP_BASE wxString
wxNow();
105 // Return path where wxWidgets is installed (mostly useful in Unices)
106 WXDLLIMPEXP_BASE
const wxChar
*wxGetInstallPrefix();
107 // Return path to wxWin data (/usr/share/wx/%{version}) (Unices)
108 WXDLLIMPEXP_BASE wxString
wxGetDataDir();
113 // Get the state of a key (true if pressed, false if not)
114 // This is generally most useful getting the state of
115 // the modifier or toggle keys.
116 WXDLLEXPORT
bool wxGetKeyState(wxKeyCode key
);
119 // Don't synthesize KeyUp events holding down a key and producing
120 // KeyDown events with autorepeat. On by default and always on
122 WXDLLEXPORT
bool wxSetDetectableAutoRepeat( bool flag
);
124 // ----------------------------------------------------------------------------
125 // Window ID management
126 // ----------------------------------------------------------------------------
128 // Generate a unique ID
129 WXDLLEXPORT
long wxNewId();
131 // Ensure subsequent IDs don't clash with this one
132 WXDLLEXPORT
void wxRegisterId(long id
);
134 // Return the current ID
135 WXDLLEXPORT
long wxGetCurrentId();
139 // ----------------------------------------------------------------------------
140 // Various conversions
141 // ----------------------------------------------------------------------------
143 // these functions are deprecated, use wxString methods instead!
144 #if WXWIN_COMPATIBILITY_2_4
146 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxFloatToStringStr
;
147 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxDoubleToStringStr
;
149 WXDLLIMPEXP_BASE
void StringToFloat(const wxChar
*s
, float *number
);
150 WXDLLIMPEXP_BASE wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
);
151 WXDLLIMPEXP_BASE
void StringToDouble(const wxChar
*s
, double *number
);
152 WXDLLIMPEXP_BASE wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
);
153 WXDLLIMPEXP_BASE
void StringToInt(const wxChar
*s
, int *number
);
154 WXDLLIMPEXP_BASE
void StringToLong(const wxChar
*s
, long *number
);
155 WXDLLIMPEXP_BASE wxChar
* IntToString(int number
);
156 WXDLLIMPEXP_BASE wxChar
* LongToString(long number
);
158 #endif // WXWIN_COMPATIBILITY_2_4
160 // Convert 2-digit hex number to decimal
161 WXDLLIMPEXP_BASE
int wxHexToDec(const wxString
& buf
);
163 // Convert decimal integer to 2-character hex string
164 WXDLLIMPEXP_BASE
void wxDecToHex(int dec
, wxChar
*buf
);
165 WXDLLIMPEXP_BASE wxString
wxDecToHex(int dec
);
167 // ----------------------------------------------------------------------------
168 // Process management
169 // ----------------------------------------------------------------------------
171 // NB: for backwards compatibility reasons the values of wxEXEC_[A]SYNC *must*
172 // be 0 and 1, don't change!
176 // execute the process asynchronously
179 // execute it synchronously, i.e. wait until it finishes
182 // under Windows, don't hide the child even if it's IO is redirected (this
183 // is done by default)
186 // under Unix, if the process is the group leader then passing wxKILL_CHILDREN to wxKill
187 // kills all children as well as pid
188 wxEXEC_MAKE_GROUP_LEADER
= 4,
190 // by default synchronous execution disables all program windows to avoid
191 // that the user interacts with the program while the child process is
192 // running, you can use this flag to prevent this from happening
196 // Execute another program.
198 // If flags contain wxEXEC_SYNC, return -1 on failure and the exit code of the
199 // process if everything was ok. Otherwise (i.e. if wxEXEC_ASYNC), return 0 on
200 // failure and the PID of the launched process if ok.
201 WXDLLIMPEXP_BASE
long wxExecute(wxChar
**argv
, int flags
= wxEXEC_ASYNC
,
202 wxProcess
*process
= (wxProcess
*) NULL
);
203 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
, int flags
= wxEXEC_ASYNC
,
204 wxProcess
*process
= (wxProcess
*) NULL
);
206 // execute the command capturing its output into an array line by line, this is
207 // always synchronous
208 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
209 wxArrayString
& output
,
212 // also capture stderr (also synchronous)
213 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
214 wxArrayString
& output
,
215 wxArrayString
& error
,
220 wxSIGNONE
= 0, // verify if the process exists under Unix
227 wxSIGIOT
= wxSIGABRT
, // another name
238 // further signals are different in meaning between different Unix systems
243 wxKILL_OK
, // no error
244 wxKILL_BAD_SIGNAL
, // no such signal
245 wxKILL_ACCESS_DENIED
, // permission denied
246 wxKILL_NO_PROCESS
, // no such process
247 wxKILL_ERROR
// another, unspecified error
252 wxKILL_NOCHILDREN
= 0, // don't kill children
253 wxKILL_CHILDREN
= 1 // kill children
258 wxSHUTDOWN_POWEROFF
, // power off the computer
259 wxSHUTDOWN_REBOOT
// shutdown and reboot
262 // Shutdown or reboot the PC
263 WXDLLIMPEXP_BASE
bool wxShutdown(wxShutdownFlags wFlags
);
265 // send the given signal to the process (only NONE and KILL are supported under
266 // Windows, all others mean TERM), return 0 if ok and -1 on error
268 // return detailed error in rc if not NULL
269 WXDLLIMPEXP_BASE
int wxKill(long pid
,
270 wxSignal sig
= wxSIGTERM
,
271 wxKillError
*rc
= NULL
,
272 int flags
= wxKILL_NOCHILDREN
);
274 // Execute a command in an interactive shell window (always synchronously)
275 // If no command then just the shell
276 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
= wxEmptyString
);
278 // As wxShell(), but must give a (non interactive) command and its output will
279 // be returned in output array
280 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
, wxArrayString
& output
);
282 // Sleep for nSecs seconds
283 WXDLLIMPEXP_BASE
void wxSleep(int nSecs
);
285 // Sleep for a given amount of milliseconds
286 WXDLLIMPEXP_BASE
void wxMilliSleep(unsigned long milliseconds
);
288 // Sleep for a given amount of microseconds
289 WXDLLIMPEXP_BASE
void wxMicroSleep(unsigned long microseconds
);
291 // Sleep for a given amount of milliseconds (old, bad name), use wxMilliSleep
292 wxDEPRECATED( WXDLLIMPEXP_BASE
void wxUsleep(unsigned long milliseconds
) );
294 // Get the process id of the current process
295 WXDLLIMPEXP_BASE
unsigned long wxGetProcessId();
297 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
298 WXDLLIMPEXP_BASE wxMemorySize
wxGetFreeMemory();
300 // should wxApp::OnFatalException() be called?
301 WXDLLIMPEXP_BASE
bool wxHandleFatalExceptions(bool doit
= true);
303 // ----------------------------------------------------------------------------
304 // Environment variables
305 // ----------------------------------------------------------------------------
307 // returns true if variable exists (value may be NULL if you just want to check
309 WXDLLIMPEXP_BASE
bool wxGetEnv(const wxString
& var
, wxString
*value
);
311 // set the env var name to the given value, return true on success
312 WXDLLIMPEXP_BASE
bool wxSetEnv(const wxString
& var
, const wxChar
*value
);
314 // remove the env var from environment
315 inline bool wxUnsetEnv(const wxString
& var
) { return wxSetEnv(var
, NULL
); }
317 // ----------------------------------------------------------------------------
318 // Network and username functions.
319 // ----------------------------------------------------------------------------
321 // NB: "char *" functions are deprecated, use wxString ones!
324 WXDLLIMPEXP_BASE
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
325 WXDLLIMPEXP_BASE wxString
wxGetEmailAddress();
328 WXDLLIMPEXP_BASE
bool wxGetHostName(wxChar
*buf
, int maxSize
);
329 WXDLLIMPEXP_BASE wxString
wxGetHostName();
332 WXDLLIMPEXP_BASE wxString
wxGetFullHostName();
333 WXDLLIMPEXP_BASE
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
335 // Get user ID e.g. jacs (this is known as login name under Unix)
336 WXDLLIMPEXP_BASE
bool wxGetUserId(wxChar
*buf
, int maxSize
);
337 WXDLLIMPEXP_BASE wxString
wxGetUserId();
339 // Get user name e.g. Julian Smart
340 WXDLLIMPEXP_BASE
bool wxGetUserName(wxChar
*buf
, int maxSize
);
341 WXDLLIMPEXP_BASE wxString
wxGetUserName();
343 // Get current Home dir and copy to dest (returns pstr->c_str())
344 WXDLLIMPEXP_BASE wxString
wxGetHomeDir();
345 WXDLLIMPEXP_BASE
const wxChar
* wxGetHomeDir(wxString
*pstr
);
347 // Get the user's home dir (caller must copy --- volatile)
348 // returns NULL is no HOME dir is known
349 #if defined(__UNIX__) && wxUSE_UNICODE
350 WXDLLIMPEXP_BASE
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
352 WXDLLIMPEXP_BASE wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
355 // get number of total/free bytes on the disk where path belongs
356 WXDLLIMPEXP_BASE
bool wxGetDiskSpace(const wxString
& path
,
357 wxLongLong
*pTotal
= NULL
,
358 wxLongLong
*pFree
= NULL
);
360 #if wxUSE_GUI // GUI only things from now on
362 // ----------------------------------------------------------------------------
363 // Menu accelerators related things
364 // ----------------------------------------------------------------------------
366 WXDLLEXPORT wxChar
* wxStripMenuCodes(const wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
367 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
370 class WXDLLEXPORT wxAcceleratorEntry
;
371 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
372 #endif // wxUSE_ACCEL
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
378 // Returns menu item id or wxNOT_FOUND if none.
379 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
381 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
382 // is always present but may be less reliable than a native version.
383 WXDLLEXPORT wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
);
384 WXDLLEXPORT wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
);
386 // NB: this function is obsolete, use wxWindow::FindWindowByLabel() instead
388 // Find the window/widget with the given title or label.
389 // Pass a parent to begin the search from, or NULL to look through
391 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
393 // NB: this function is obsolete, use wxWindow::FindWindowByName() instead
395 // Find window by name, and if that fails, by label.
396 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
398 // ----------------------------------------------------------------------------
399 // Message/event queue helpers
400 // ----------------------------------------------------------------------------
402 // Yield to other apps/messages and disable user input
403 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
, bool onlyIfNeeded
= false);
405 // Enable or disable input to all top level windows
406 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= true);
408 // Check whether this window wants to process messages, e.g. Stop button
409 // in long calculations.
410 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
412 // Consume all events until no more left
413 WXDLLEXPORT
void wxFlushEvents();
415 // a class which disables all windows (except, may be, thegiven one) in its
416 // ctor and enables them back in its dtor
417 class WXDLLEXPORT wxWindowDisabler
420 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
424 wxWindowList
*m_winDisabled
;
426 DECLARE_NO_COPY_CLASS(wxWindowDisabler
)
429 // ----------------------------------------------------------------------------
431 // ----------------------------------------------------------------------------
433 // Set the cursor to the busy cursor for all windows
434 class WXDLLEXPORT wxCursor
;
435 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
436 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
438 // Restore cursor to normal
439 WXDLLEXPORT
void wxEndBusyCursor();
441 // true if we're between the above two calls
442 WXDLLEXPORT
bool wxIsBusy();
444 // Convenience class so we can just create a wxBusyCursor object on the stack
445 class WXDLLEXPORT wxBusyCursor
448 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
449 { wxBeginBusyCursor(cursor
); }
451 { wxEndBusyCursor(); }
453 // FIXME: These two methods are currently only implemented (and needed?)
454 // in wxGTK. BusyCursor handling should probably be moved to
455 // common code since the wxGTK and wxMSW implementations are very
456 // similar except for wxMSW using HCURSOR directly instead of
458 static const wxCursor
&GetStoredCursor();
459 static const wxCursor
GetBusyCursor();
463 // ----------------------------------------------------------------------------
464 // Reading and writing resources (eg WIN.INI, .Xdefaults)
465 // ----------------------------------------------------------------------------
468 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
469 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
470 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
471 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
473 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
474 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
475 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
476 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
477 #endif // wxUSE_RESOURCES
479 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
481 // MSW only: get user-defined resource from the .res file.
482 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
484 WXDLLEXPORT
extern const wxChar
* wxUserResourceStr
;
485 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
488 // ----------------------------------------------------------------------------
489 // Display and colorss (X only)
490 // ----------------------------------------------------------------------------
493 void *wxGetDisplay();
497 WXDisplay
*wxGetDisplay();
498 bool wxSetDisplay(const wxString
& display_name
);
499 wxString
wxGetDisplayName();
504 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
505 // The resulting warnings are switched off here
506 #pragma message disable nosimpint
508 // #include <X11/Xlib.h>
510 #pragma message enable nosimpint
517 // ----------------------------------------------------------------------------
518 // wxYield(): these functions are obsolete, please use wxApp methods instead!
519 // ----------------------------------------------------------------------------
521 // Yield to other apps/messages
522 WXDLLIMPEXP_BASE
bool wxYield();
524 // Like wxYield, but fails silently if the yield is recursive.
525 WXDLLIMPEXP_BASE
bool wxYieldIfNeeded();
527 // ----------------------------------------------------------------------------
528 // Error message functions used by wxWidgets (deprecated, use wxLog)
529 // ----------------------------------------------------------------------------
531 #if WXWIN_COMPATIBILITY_2_2
533 // Format a message on the standard error (UNIX) or the debugging
535 WXDLLIMPEXP_BASE
void wxDebugMsg(const wxChar
*fmt
...) ATTRIBUTE_PRINTF_1
;
537 // Non-fatal error (continues)
538 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxInternalErrorStr
;
539 WXDLLIMPEXP_BASE
void wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
541 // Fatal error (exits)
542 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxFatalErrorStr
;
543 WXDLLIMPEXP_BASE
void wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);
545 #endif // WXWIN_COMPATIBILITY_2_2