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 wxDEPRECATED( 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 #if !defined __EMX__ && \
94 (defined __WXMOTIF__ || defined __WXGTK__ || defined __WXX11__)
95 WXDLLIMPEXP_CORE
void wxBell();
97 WXDLLIMPEXP_BASE
void wxBell();
100 // Get OS description as a user-readable string
101 WXDLLIMPEXP_BASE wxString
wxGetOsDescription();
104 WXDLLIMPEXP_BASE
int wxGetOsVersion(int *majorVsn
= (int *) NULL
,
105 int *minorVsn
= (int *) NULL
);
107 // Return a string with the current date/time
108 WXDLLIMPEXP_BASE wxString
wxNow();
110 // Return path where wxWidgets is installed (mostly useful in Unices)
111 WXDLLIMPEXP_BASE
const wxChar
*wxGetInstallPrefix();
112 // Return path to wxWin data (/usr/share/wx/%{version}) (Unices)
113 WXDLLIMPEXP_BASE wxString
wxGetDataDir();
118 // Get the state of a key (true if pressed, false if not)
119 // This is generally most useful getting the state of
120 // the modifier or toggle keys.
121 WXDLLEXPORT
bool wxGetKeyState(wxKeyCode key
);
124 // Don't synthesize KeyUp events holding down a key and producing
125 // KeyDown events with autorepeat. On by default and always on
127 WXDLLEXPORT
bool wxSetDetectableAutoRepeat( bool flag
);
129 // ----------------------------------------------------------------------------
130 // Window ID management
131 // ----------------------------------------------------------------------------
133 // Generate a unique ID
134 WXDLLEXPORT
long wxNewId();
136 // Ensure subsequent IDs don't clash with this one
137 WXDLLEXPORT
void wxRegisterId(long id
);
139 // Return the current ID
140 WXDLLEXPORT
long wxGetCurrentId();
144 // ----------------------------------------------------------------------------
145 // Various conversions
146 // ----------------------------------------------------------------------------
148 // these functions are deprecated, use wxString methods instead!
149 #if WXWIN_COMPATIBILITY_2_4
151 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxFloatToStringStr
;
152 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxDoubleToStringStr
;
154 wxDEPRECATED( WXDLLIMPEXP_BASE
void StringToFloat(const wxChar
*s
, float *number
) );
155 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
) );
156 wxDEPRECATED( WXDLLIMPEXP_BASE
void StringToDouble(const wxChar
*s
, double *number
) );
157 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
) );
158 wxDEPRECATED( WXDLLIMPEXP_BASE
void StringToInt(const wxChar
*s
, int *number
) );
159 wxDEPRECATED( WXDLLIMPEXP_BASE
void StringToLong(const wxChar
*s
, long *number
) );
160 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* IntToString(int number
) );
161 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* LongToString(long number
) );
163 #endif // WXWIN_COMPATIBILITY_2_4
165 // Convert 2-digit hex number to decimal
166 WXDLLIMPEXP_BASE
int wxHexToDec(const wxString
& buf
);
168 // Convert decimal integer to 2-character hex string
169 WXDLLIMPEXP_BASE
void wxDecToHex(int dec
, wxChar
*buf
);
170 WXDLLIMPEXP_BASE wxString
wxDecToHex(int dec
);
172 // ----------------------------------------------------------------------------
173 // Process management
174 // ----------------------------------------------------------------------------
176 // NB: for backwards compatibility reasons the values of wxEXEC_[A]SYNC *must*
177 // be 0 and 1, don't change!
181 // execute the process asynchronously
184 // execute it synchronously, i.e. wait until it finishes
187 // under Windows, don't hide the child even if it's IO is redirected (this
188 // is done by default)
191 // under Unix, if the process is the group leader then passing wxKILL_CHILDREN to wxKill
192 // kills all children as well as pid
193 wxEXEC_MAKE_GROUP_LEADER
= 4,
195 // by default synchronous execution disables all program windows to avoid
196 // that the user interacts with the program while the child process is
197 // running, you can use this flag to prevent this from happening
201 // Execute another program.
203 // If flags contain wxEXEC_SYNC, return -1 on failure and the exit code of the
204 // process if everything was ok. Otherwise (i.e. if wxEXEC_ASYNC), return 0 on
205 // failure and the PID of the launched process if ok.
206 WXDLLIMPEXP_BASE
long wxExecute(wxChar
**argv
, int flags
= wxEXEC_ASYNC
,
207 wxProcess
*process
= (wxProcess
*) NULL
);
208 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
, int flags
= wxEXEC_ASYNC
,
209 wxProcess
*process
= (wxProcess
*) NULL
);
211 // execute the command capturing its output into an array line by line, this is
212 // always synchronous
213 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
214 wxArrayString
& output
,
217 // also capture stderr (also synchronous)
218 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
219 wxArrayString
& output
,
220 wxArrayString
& error
,
225 wxSIGNONE
= 0, // verify if the process exists under Unix
232 wxSIGIOT
= wxSIGABRT
, // another name
243 // further signals are different in meaning between different Unix systems
248 wxKILL_OK
, // no error
249 wxKILL_BAD_SIGNAL
, // no such signal
250 wxKILL_ACCESS_DENIED
, // permission denied
251 wxKILL_NO_PROCESS
, // no such process
252 wxKILL_ERROR
// another, unspecified error
257 wxKILL_NOCHILDREN
= 0, // don't kill children
258 wxKILL_CHILDREN
= 1 // kill children
263 wxSHUTDOWN_POWEROFF
, // power off the computer
264 wxSHUTDOWN_REBOOT
// shutdown and reboot
267 // Shutdown or reboot the PC
268 WXDLLIMPEXP_BASE
bool wxShutdown(wxShutdownFlags wFlags
);
277 WXDLLIMPEXP_BASE wxPowerType
wxGetPowerType();
281 wxBATTERY_NORMAL_STATE
, // system is fully usable
282 wxBATTERY_LOW_STATE
, // start to worry
283 wxBATTERY_CRITICAL_STATE
, // save quickly
284 wxBATTERY_SHUTDOWN_STATE
, // too late
285 wxBATTERY_UNKNOWN_STATE
288 WXDLLIMPEXP_BASE wxBatteryState
wxGetBatteryState();
290 // send the given signal to the process (only NONE and KILL are supported under
291 // Windows, all others mean TERM), return 0 if ok and -1 on error
293 // return detailed error in rc if not NULL
294 WXDLLIMPEXP_BASE
int wxKill(long pid
,
295 wxSignal sig
= wxSIGTERM
,
296 wxKillError
*rc
= NULL
,
297 int flags
= wxKILL_NOCHILDREN
);
299 // Execute a command in an interactive shell window (always synchronously)
300 // If no command then just the shell
301 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
= wxEmptyString
);
303 // As wxShell(), but must give a (non interactive) command and its output will
304 // be returned in output array
305 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
, wxArrayString
& output
);
307 // Sleep for nSecs seconds
308 WXDLLIMPEXP_BASE
void wxSleep(int nSecs
);
310 // Sleep for a given amount of milliseconds
311 WXDLLIMPEXP_BASE
void wxMilliSleep(unsigned long milliseconds
);
313 // Sleep for a given amount of microseconds
314 WXDLLIMPEXP_BASE
void wxMicroSleep(unsigned long microseconds
);
316 // Sleep for a given amount of milliseconds (old, bad name), use wxMilliSleep
317 wxDEPRECATED( WXDLLIMPEXP_BASE
void wxUsleep(unsigned long milliseconds
) );
319 // Get the process id of the current process
320 WXDLLIMPEXP_BASE
unsigned long wxGetProcessId();
322 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
323 WXDLLIMPEXP_BASE wxMemorySize
wxGetFreeMemory();
325 #if wxUSE_ON_FATAL_EXCEPTION
327 // should wxApp::OnFatalException() be called?
328 WXDLLIMPEXP_BASE
bool wxHandleFatalExceptions(bool doit
= true);
330 #endif // wxUSE_ON_FATAL_EXCEPTION
332 #if wxABI_VERSION >= 20601
333 // Launch url in the user's default internet browser
334 WXDLLIMPEXP_BASE
bool wxLaunchDefaultBrowser(const wxString
& url
);
337 // ----------------------------------------------------------------------------
338 // Environment variables
339 // ----------------------------------------------------------------------------
341 // returns true if variable exists (value may be NULL if you just want to check
343 WXDLLIMPEXP_BASE
bool wxGetEnv(const wxString
& var
, wxString
*value
);
345 // set the env var name to the given value, return true on success
346 WXDLLIMPEXP_BASE
bool wxSetEnv(const wxString
& var
, const wxChar
*value
);
348 // remove the env var from environment
349 inline bool wxUnsetEnv(const wxString
& var
) { return wxSetEnv(var
, NULL
); }
351 // ----------------------------------------------------------------------------
352 // Network and username functions.
353 // ----------------------------------------------------------------------------
355 // NB: "char *" functions are deprecated, use wxString ones!
358 WXDLLIMPEXP_BASE
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
359 WXDLLIMPEXP_BASE wxString
wxGetEmailAddress();
362 WXDLLIMPEXP_BASE
bool wxGetHostName(wxChar
*buf
, int maxSize
);
363 WXDLLIMPEXP_BASE wxString
wxGetHostName();
366 WXDLLIMPEXP_BASE wxString
wxGetFullHostName();
367 WXDLLIMPEXP_BASE
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
369 // Get user ID e.g. jacs (this is known as login name under Unix)
370 WXDLLIMPEXP_BASE
bool wxGetUserId(wxChar
*buf
, int maxSize
);
371 WXDLLIMPEXP_BASE wxString
wxGetUserId();
373 // Get user name e.g. Julian Smart
374 WXDLLIMPEXP_BASE
bool wxGetUserName(wxChar
*buf
, int maxSize
);
375 WXDLLIMPEXP_BASE wxString
wxGetUserName();
377 // Get current Home dir and copy to dest (returns pstr->c_str())
378 WXDLLIMPEXP_BASE wxString
wxGetHomeDir();
379 WXDLLIMPEXP_BASE
const wxChar
* wxGetHomeDir(wxString
*pstr
);
381 // Get the user's home dir (caller must copy --- volatile)
382 // returns NULL is no HOME dir is known
383 #if defined(__UNIX__) && wxUSE_UNICODE
384 WXDLLIMPEXP_BASE
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
386 WXDLLIMPEXP_BASE wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
389 // get number of total/free bytes on the disk where path belongs
390 WXDLLIMPEXP_BASE
bool wxGetDiskSpace(const wxString
& path
,
391 wxLongLong
*pTotal
= NULL
,
392 wxLongLong
*pFree
= NULL
);
394 #if wxUSE_GUI // GUI only things from now on
396 // ----------------------------------------------------------------------------
397 // Menu accelerators related things
398 // ----------------------------------------------------------------------------
400 WXDLLEXPORT wxChar
* wxStripMenuCodes(const wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
401 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
404 class WXDLLEXPORT wxAcceleratorEntry
;
405 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
406 #endif // wxUSE_ACCEL
408 // ----------------------------------------------------------------------------
410 // ----------------------------------------------------------------------------
412 // Returns menu item id or wxNOT_FOUND if none.
413 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
415 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
416 // is always present but may be less reliable than a native version.
417 WXDLLEXPORT wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
);
418 WXDLLEXPORT wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
);
420 // NB: this function is obsolete, use wxWindow::FindWindowByLabel() instead
422 // Find the window/widget with the given title or label.
423 // Pass a parent to begin the search from, or NULL to look through
425 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
427 // NB: this function is obsolete, use wxWindow::FindWindowByName() instead
429 // Find window by name, and if that fails, by label.
430 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
432 // ----------------------------------------------------------------------------
433 // Message/event queue helpers
434 // ----------------------------------------------------------------------------
436 // Yield to other apps/messages and disable user input
437 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
, bool onlyIfNeeded
= false);
439 // Enable or disable input to all top level windows
440 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= true);
442 // Check whether this window wants to process messages, e.g. Stop button
443 // in long calculations.
444 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
446 // Consume all events until no more left
447 WXDLLEXPORT
void wxFlushEvents();
449 // a class which disables all windows (except, may be, thegiven one) in its
450 // ctor and enables them back in its dtor
451 class WXDLLEXPORT wxWindowDisabler
454 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
458 wxWindowList
*m_winDisabled
;
460 DECLARE_NO_COPY_CLASS(wxWindowDisabler
)
463 // ----------------------------------------------------------------------------
465 // ----------------------------------------------------------------------------
467 // Set the cursor to the busy cursor for all windows
468 class WXDLLEXPORT wxCursor
;
469 extern WXDLLEXPORT_DATA(wxCursor
*) wxHOURGLASS_CURSOR
;
470 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
472 // Restore cursor to normal
473 WXDLLEXPORT
void wxEndBusyCursor();
475 // true if we're between the above two calls
476 WXDLLEXPORT
bool wxIsBusy();
478 // Convenience class so we can just create a wxBusyCursor object on the stack
479 class WXDLLEXPORT wxBusyCursor
482 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
483 { wxBeginBusyCursor(cursor
); }
485 { wxEndBusyCursor(); }
487 // FIXME: These two methods are currently only implemented (and needed?)
488 // in wxGTK. BusyCursor handling should probably be moved to
489 // common code since the wxGTK and wxMSW implementations are very
490 // similar except for wxMSW using HCURSOR directly instead of
492 static const wxCursor
&GetStoredCursor();
493 static const wxCursor
GetBusyCursor();
497 // ----------------------------------------------------------------------------
498 // Reading and writing resources (eg WIN.INI, .Xdefaults)
499 // ----------------------------------------------------------------------------
502 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
503 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
504 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
505 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
507 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
508 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
509 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
510 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
511 #endif // wxUSE_RESOURCES
513 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
515 // MSW only: get user-defined resource from the .res file.
516 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
518 extern WXDLLEXPORT
const wxChar
* wxUserResourceStr
;
519 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
522 // ----------------------------------------------------------------------------
523 // Display and colorss (X only)
524 // ----------------------------------------------------------------------------
527 void *wxGetDisplay();
531 WXDLLIMPEXP_CORE WXDisplay
*wxGetDisplay();
532 WXDLLIMPEXP_CORE
bool wxSetDisplay(const wxString
& display_name
);
533 WXDLLIMPEXP_CORE wxString
wxGetDisplayName();
538 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
539 // The resulting warnings are switched off here
540 #pragma message disable nosimpint
542 // #include <X11/Xlib.h>
544 #pragma message enable nosimpint
551 // ----------------------------------------------------------------------------
552 // wxYield(): these functions are obsolete, please use wxApp methods instead!
553 // ----------------------------------------------------------------------------
555 // Yield to other apps/messages
556 WXDLLIMPEXP_BASE
bool wxYield();
558 // Like wxYield, but fails silently if the yield is recursive.
559 WXDLLIMPEXP_BASE
bool wxYieldIfNeeded();
561 // ----------------------------------------------------------------------------
562 // Error message functions used by wxWidgets (deprecated, use wxLog)
563 // ----------------------------------------------------------------------------
565 #if WXWIN_COMPATIBILITY_2_2
567 // Format a message on the standard error (UNIX) or the debugging
569 wxDEPRECATED( WXDLLIMPEXP_BASE
void wxDebugMsg(const wxChar
*fmt
...) ATTRIBUTE_PRINTF_1
);
571 // Non-fatal error (continues)
572 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxInternalErrorStr
;
573 wxDEPRECATED( WXDLLIMPEXP_BASE
void wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
) );
575 // Fatal error (exits)
576 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxFatalErrorStr
;
577 wxDEPRECATED( WXDLLIMPEXP_BASE
void wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
) );
579 #endif // WXWIN_COMPATIBILITY_2_2