1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Miscellaneous utilities
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/object.h"
21 #include "wx/filefn.h"
23 class WXDLLIMPEXP_BASE wxArrayString
;
25 // need this for wxGetDiskSpace() as we can't, unfortunately, forward declare
27 #include "wx/longlong.h"
38 // ----------------------------------------------------------------------------
39 // Forward declaration
40 // ----------------------------------------------------------------------------
42 class WXDLLIMPEXP_CORE wxProcess
;
43 class WXDLLIMPEXP_CORE wxFrame
;
44 class WXDLLIMPEXP_CORE wxWindow
;
45 class WXDLLIMPEXP_CORE wxWindowList
;
46 class WXDLLIMPEXP_CORE wxPoint
;
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
53 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
55 // wxGetFreeMemory can return huge amount of memory on 32-bit platforms as well
56 // so to always use long long for its result type on all platforms which
59 typedef wxLongLong wxMemorySize
;
61 typedef long wxMemorySize
;
64 // ----------------------------------------------------------------------------
65 // String functions (deprecated, use wxString)
66 // ----------------------------------------------------------------------------
68 // Make a copy of this string using 'new'
69 #if WXWIN_COMPATIBILITY_2_4
70 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* copystring(const wxChar
*s
) );
73 // A shorter way of using strcmp
74 #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
76 // ----------------------------------------------------------------------------
77 // Miscellaneous functions
78 // ----------------------------------------------------------------------------
81 #if !defined __EMX__ && \
82 (defined __WXMOTIF__ || defined __WXGTK__ || defined __WXX11__)
83 WXDLLIMPEXP_CORE
void wxBell();
85 WXDLLIMPEXP_BASE
void wxBell();
88 // Get OS description as a user-readable string
89 WXDLLIMPEXP_BASE wxString
wxGetOsDescription();
92 WXDLLIMPEXP_BASE
int wxGetOsVersion(int *majorVsn
= (int *) NULL
,
93 int *minorVsn
= (int *) NULL
);
95 // Return a string with the current date/time
96 WXDLLIMPEXP_BASE wxString
wxNow();
98 // Return path where wxWidgets is installed (mostly useful in Unices)
99 WXDLLIMPEXP_BASE
const wxChar
*wxGetInstallPrefix();
100 // Return path to wxWin data (/usr/share/wx/%{version}) (Unices)
101 WXDLLIMPEXP_BASE wxString
wxGetDataDir();
106 // Get the state of a key (true if pressed, false if not)
107 // This is generally most useful getting the state of
108 // the modifier or toggle keys.
109 WXDLLEXPORT
bool wxGetKeyState(wxKeyCode key
);
112 // Don't synthesize KeyUp events holding down a key and producing
113 // KeyDown events with autorepeat. On by default and always on
115 WXDLLEXPORT
bool wxSetDetectableAutoRepeat( bool flag
);
117 // ----------------------------------------------------------------------------
118 // Window ID management
119 // ----------------------------------------------------------------------------
121 // Generate a unique ID
122 WXDLLEXPORT
long wxNewId();
124 // Ensure subsequent IDs don't clash with this one
125 WXDLLEXPORT
void wxRegisterId(long id
);
127 // Return the current ID
128 WXDLLEXPORT
long wxGetCurrentId();
132 // ----------------------------------------------------------------------------
133 // Various conversions
134 // ----------------------------------------------------------------------------
136 // these functions are deprecated, use wxString methods instead!
137 #if WXWIN_COMPATIBILITY_2_4
139 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxFloatToStringStr
;
140 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxDoubleToStringStr
;
142 wxDEPRECATED( WXDLLIMPEXP_BASE
void StringToFloat(const wxChar
*s
, float *number
) );
143 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
) );
144 wxDEPRECATED( WXDLLIMPEXP_BASE
void StringToDouble(const wxChar
*s
, double *number
) );
145 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
) );
146 wxDEPRECATED( WXDLLIMPEXP_BASE
void StringToInt(const wxChar
*s
, int *number
) );
147 wxDEPRECATED( WXDLLIMPEXP_BASE
void StringToLong(const wxChar
*s
, long *number
) );
148 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* IntToString(int number
) );
149 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* LongToString(long number
) );
151 #endif // WXWIN_COMPATIBILITY_2_4
153 // Convert 2-digit hex number to decimal
154 WXDLLIMPEXP_BASE
int wxHexToDec(const wxString
& buf
);
156 // Convert decimal integer to 2-character hex string
157 WXDLLIMPEXP_BASE
void wxDecToHex(int dec
, wxChar
*buf
);
158 WXDLLIMPEXP_BASE wxString
wxDecToHex(int dec
);
160 // ----------------------------------------------------------------------------
161 // Process management
162 // ----------------------------------------------------------------------------
164 // NB: for backwards compatibility reasons the values of wxEXEC_[A]SYNC *must*
165 // be 0 and 1, don't change!
169 // execute the process asynchronously
172 // execute it synchronously, i.e. wait until it finishes
175 // under Windows, don't hide the child even if it's IO is redirected (this
176 // is done by default)
179 // under Unix, if the process is the group leader then passing wxKILL_CHILDREN to wxKill
180 // kills all children as well as pid
181 wxEXEC_MAKE_GROUP_LEADER
= 4,
183 // by default synchronous execution disables all program windows to avoid
184 // that the user interacts with the program while the child process is
185 // running, you can use this flag to prevent this from happening
189 // Execute another program.
191 // If flags contain wxEXEC_SYNC, return -1 on failure and the exit code of the
192 // process if everything was ok. Otherwise (i.e. if wxEXEC_ASYNC), return 0 on
193 // failure and the PID of the launched process if ok.
194 WXDLLIMPEXP_BASE
long wxExecute(wxChar
**argv
, int flags
= wxEXEC_ASYNC
,
195 wxProcess
*process
= (wxProcess
*) NULL
);
196 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
, int flags
= wxEXEC_ASYNC
,
197 wxProcess
*process
= (wxProcess
*) NULL
);
199 // execute the command capturing its output into an array line by line, this is
200 // always synchronous
201 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
202 wxArrayString
& output
,
205 // also capture stderr (also synchronous)
206 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
207 wxArrayString
& output
,
208 wxArrayString
& error
,
211 #if defined(__WXMSW__) && wxUSE_IPC
212 // ask a DDE server to execute the DDE request with given parameters
213 WXDLLIMPEXP_BASE
bool wxExecuteDDE(const wxString
& ddeServer
,
214 const wxString
& ddeTopic
,
215 const wxString
& ddeCommand
);
216 #endif // __WXMSW__ && wxUSE_IPC
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
);
272 WXDLLIMPEXP_BASE wxPowerType
wxGetPowerType();
276 wxBATTERY_NORMAL_STATE
, // system is fully usable
277 wxBATTERY_LOW_STATE
, // start to worry
278 wxBATTERY_CRITICAL_STATE
, // save quickly
279 wxBATTERY_SHUTDOWN_STATE
, // too late
280 wxBATTERY_UNKNOWN_STATE
283 WXDLLIMPEXP_BASE wxBatteryState
wxGetBatteryState();
285 // send the given signal to the process (only NONE and KILL are supported under
286 // Windows, all others mean TERM), return 0 if ok and -1 on error
288 // return detailed error in rc if not NULL
289 WXDLLIMPEXP_BASE
int wxKill(long pid
,
290 wxSignal sig
= wxSIGTERM
,
291 wxKillError
*rc
= NULL
,
292 int flags
= wxKILL_NOCHILDREN
);
294 // Execute a command in an interactive shell window (always synchronously)
295 // If no command then just the shell
296 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
= wxEmptyString
);
298 // As wxShell(), but must give a (non interactive) command and its output will
299 // be returned in output array
300 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
, wxArrayString
& output
);
302 // Sleep for nSecs seconds
303 WXDLLIMPEXP_BASE
void wxSleep(int nSecs
);
305 // Sleep for a given amount of milliseconds
306 WXDLLIMPEXP_BASE
void wxMilliSleep(unsigned long milliseconds
);
308 // Sleep for a given amount of microseconds
309 WXDLLIMPEXP_BASE
void wxMicroSleep(unsigned long microseconds
);
311 // Sleep for a given amount of milliseconds (old, bad name), use wxMilliSleep
312 wxDEPRECATED( WXDLLIMPEXP_BASE
void wxUsleep(unsigned long milliseconds
) );
314 // Get the process id of the current process
315 WXDLLIMPEXP_BASE
unsigned long wxGetProcessId();
317 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
318 WXDLLIMPEXP_BASE wxMemorySize
wxGetFreeMemory();
320 #if wxUSE_ON_FATAL_EXCEPTION
322 // should wxApp::OnFatalException() be called?
323 WXDLLIMPEXP_BASE
bool wxHandleFatalExceptions(bool doit
= true);
325 #endif // wxUSE_ON_FATAL_EXCEPTION
327 // flags for wxLaunchDefaultBrowser
330 wxBROWSER_NEW_WINDOW
= 1
333 // Launch url in the user's default internet browser
334 WXDLLIMPEXP_BASE
bool wxLaunchDefaultBrowser(const wxString
& url
, int flags
= 0);
336 // ----------------------------------------------------------------------------
337 // Environment variables
338 // ----------------------------------------------------------------------------
340 // returns true if variable exists (value may be NULL if you just want to check
342 WXDLLIMPEXP_BASE
bool wxGetEnv(const wxString
& var
, wxString
*value
);
344 // set the env var name to the given value, return true on success
345 WXDLLIMPEXP_BASE
bool wxSetEnv(const wxString
& var
, const wxChar
*value
);
347 // remove the env var from environment
348 inline bool wxUnsetEnv(const wxString
& var
) { return wxSetEnv(var
, NULL
); }
350 // ----------------------------------------------------------------------------
351 // Network and username functions.
352 // ----------------------------------------------------------------------------
354 // NB: "char *" functions are deprecated, use wxString ones!
357 WXDLLIMPEXP_BASE
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
358 WXDLLIMPEXP_BASE wxString
wxGetEmailAddress();
361 WXDLLIMPEXP_BASE
bool wxGetHostName(wxChar
*buf
, int maxSize
);
362 WXDLLIMPEXP_BASE wxString
wxGetHostName();
365 WXDLLIMPEXP_BASE wxString
wxGetFullHostName();
366 WXDLLIMPEXP_BASE
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
368 // Get user ID e.g. jacs (this is known as login name under Unix)
369 WXDLLIMPEXP_BASE
bool wxGetUserId(wxChar
*buf
, int maxSize
);
370 WXDLLIMPEXP_BASE wxString
wxGetUserId();
372 // Get user name e.g. Julian Smart
373 WXDLLIMPEXP_BASE
bool wxGetUserName(wxChar
*buf
, int maxSize
);
374 WXDLLIMPEXP_BASE wxString
wxGetUserName();
376 // Get current Home dir and copy to dest (returns pstr->c_str())
377 WXDLLIMPEXP_BASE wxString
wxGetHomeDir();
378 WXDLLIMPEXP_BASE
const wxChar
* wxGetHomeDir(wxString
*pstr
);
380 // Get the user's home dir (caller must copy --- volatile)
381 // returns NULL is no HOME dir is known
382 #if defined(__UNIX__) && wxUSE_UNICODE
383 WXDLLIMPEXP_BASE
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
385 WXDLLIMPEXP_BASE wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
388 // get number of total/free bytes on the disk where path belongs
389 WXDLLIMPEXP_BASE
bool wxGetDiskSpace(const wxString
& path
,
390 wxLongLong
*pTotal
= NULL
,
391 wxLongLong
*pFree
= NULL
);
393 #if wxUSE_GUI // GUI only things from now on
395 // ----------------------------------------------------------------------------
396 // Menu accelerators related things
397 // ----------------------------------------------------------------------------
399 WXDLLEXPORT wxChar
* wxStripMenuCodes(const wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
400 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
403 class WXDLLEXPORT wxAcceleratorEntry
;
404 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
405 #endif // wxUSE_ACCEL
407 // ----------------------------------------------------------------------------
409 // ----------------------------------------------------------------------------
411 // Returns menu item id or wxNOT_FOUND if none.
412 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
414 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
415 // is always present but may be less reliable than a native version.
416 WXDLLEXPORT wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
);
417 WXDLLEXPORT wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
);
419 // NB: this function is obsolete, use wxWindow::FindWindowByLabel() instead
421 // Find the window/widget with the given title or label.
422 // Pass a parent to begin the search from, or NULL to look through
424 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
426 // NB: this function is obsolete, use wxWindow::FindWindowByName() instead
428 // Find window by name, and if that fails, by label.
429 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
431 // ----------------------------------------------------------------------------
432 // Message/event queue helpers
433 // ----------------------------------------------------------------------------
435 // Yield to other apps/messages and disable user input
436 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
, bool onlyIfNeeded
= false);
438 // Enable or disable input to all top level windows
439 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= true);
441 // Check whether this window wants to process messages, e.g. Stop button
442 // in long calculations.
443 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
445 // Consume all events until no more left
446 WXDLLEXPORT
void wxFlushEvents();
448 // a class which disables all windows (except, may be, thegiven one) in its
449 // ctor and enables them back in its dtor
450 class WXDLLEXPORT wxWindowDisabler
453 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
457 wxWindowList
*m_winDisabled
;
459 DECLARE_NO_COPY_CLASS(wxWindowDisabler
)
462 // ----------------------------------------------------------------------------
464 // ----------------------------------------------------------------------------
466 // Set the cursor to the busy cursor for all windows
467 class WXDLLEXPORT wxCursor
;
468 extern WXDLLEXPORT_DATA(wxCursor
*) wxHOURGLASS_CURSOR
;
469 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
471 // Restore cursor to normal
472 WXDLLEXPORT
void wxEndBusyCursor();
474 // true if we're between the above two calls
475 WXDLLEXPORT
bool wxIsBusy();
477 // Convenience class so we can just create a wxBusyCursor object on the stack
478 class WXDLLEXPORT wxBusyCursor
481 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
482 { wxBeginBusyCursor(cursor
); }
484 { wxEndBusyCursor(); }
486 // FIXME: These two methods are currently only implemented (and needed?)
487 // in wxGTK. BusyCursor handling should probably be moved to
488 // common code since the wxGTK and wxMSW implementations are very
489 // similar except for wxMSW using HCURSOR directly instead of
491 static const wxCursor
&GetStoredCursor();
492 static const wxCursor
GetBusyCursor();
496 // ----------------------------------------------------------------------------
497 // Reading and writing resources (eg WIN.INI, .Xdefaults)
498 // ----------------------------------------------------------------------------
501 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
502 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
503 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
504 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
506 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
507 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
508 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
509 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
510 #endif // wxUSE_RESOURCES
512 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
514 // MSW only: get user-defined resource from the .res file.
515 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
517 extern WXDLLEXPORT
const wxChar
* wxUserResourceStr
;
518 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
521 // ----------------------------------------------------------------------------
522 // Display and colorss (X only)
523 // ----------------------------------------------------------------------------
526 void *wxGetDisplay();
530 WXDLLIMPEXP_CORE WXDisplay
*wxGetDisplay();
531 WXDLLIMPEXP_CORE
bool wxSetDisplay(const wxString
& display_name
);
532 WXDLLIMPEXP_CORE wxString
wxGetDisplayName();
537 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
538 // The resulting warnings are switched off here
539 #pragma message disable nosimpint
541 // #include <X11/Xlib.h>
543 #pragma message enable nosimpint
550 // ----------------------------------------------------------------------------
551 // wxYield(): these functions are obsolete, please use wxApp methods instead!
552 // ----------------------------------------------------------------------------
554 // Yield to other apps/messages
555 WXDLLIMPEXP_BASE
bool wxYield();
557 // Like wxYield, but fails silently if the yield is recursive.
558 WXDLLIMPEXP_BASE
bool wxYieldIfNeeded();
560 // ----------------------------------------------------------------------------
561 // Error message functions used by wxWidgets (deprecated, use wxLog)
562 // ----------------------------------------------------------------------------