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"
36 // ----------------------------------------------------------------------------
37 // Forward declaration
38 // ----------------------------------------------------------------------------
40 class WXDLLIMPEXP_CORE wxProcess
;
41 class WXDLLIMPEXP_CORE wxFrame
;
42 class WXDLLIMPEXP_CORE wxWindow
;
43 class WXDLLIMPEXP_CORE wxWindowList
;
44 class WXDLLIMPEXP_CORE wxPoint
;
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
51 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
53 // wxGetFreeMemory can return huge amount of memory on 64-bit platforms
54 // define wxMemorySize according to the type which best fits your platform
55 #if wxUSE_LONGLONG && defined(__WIN64__)
56 // 64 bit Windowses have sizeof(long) only 32 bit long
57 // we need to use wxLongLong to express memory sizes
58 #define wxMemorySize wxLongLong
60 // 64 bit UNIX has sizeof(long) = 64
61 // assume 32 bit platforms cannnot return more than 32bits of
62 #define wxMemorySize long
65 // ----------------------------------------------------------------------------
66 // String functions (deprecated, use wxString)
67 // ----------------------------------------------------------------------------
69 // Make a copy of this string using 'new'
70 #if WXWIN_COMPATIBILITY_2_4
71 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* copystring(const wxChar
*s
) );
74 // A shorter way of using strcmp
75 #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
77 // ----------------------------------------------------------------------------
78 // Miscellaneous functions
79 // ----------------------------------------------------------------------------
82 #if !defined __EMX__ && \
83 (defined __WXMOTIF__ || defined __WXGTK__ || defined __WXX11__)
84 WXDLLIMPEXP_CORE
void wxBell();
86 WXDLLIMPEXP_BASE
void wxBell();
89 // Get OS description as a user-readable string
90 WXDLLIMPEXP_BASE wxString
wxGetOsDescription();
93 WXDLLIMPEXP_BASE
int wxGetOsVersion(int *majorVsn
= (int *) NULL
,
94 int *minorVsn
= (int *) NULL
);
96 // Return a string with the current date/time
97 WXDLLIMPEXP_BASE wxString
wxNow();
99 // Return path where wxWidgets is installed (mostly useful in Unices)
100 WXDLLIMPEXP_BASE
const wxChar
*wxGetInstallPrefix();
101 // Return path to wxWin data (/usr/share/wx/%{version}) (Unices)
102 WXDLLIMPEXP_BASE wxString
wxGetDataDir();
107 // Get the state of a key (true if pressed, false if not)
108 // This is generally most useful getting the state of
109 // the modifier or toggle keys.
110 WXDLLEXPORT
bool wxGetKeyState(wxKeyCode key
);
113 // Don't synthesize KeyUp events holding down a key and producing
114 // KeyDown events with autorepeat. On by default and always on
116 WXDLLEXPORT
bool wxSetDetectableAutoRepeat( bool flag
);
118 // ----------------------------------------------------------------------------
119 // Window ID management
120 // ----------------------------------------------------------------------------
122 // Generate a unique ID
123 WXDLLEXPORT
long wxNewId();
125 // Ensure subsequent IDs don't clash with this one
126 WXDLLEXPORT
void wxRegisterId(long id
);
128 // Return the current ID
129 WXDLLEXPORT
long wxGetCurrentId();
133 // ----------------------------------------------------------------------------
134 // Various conversions
135 // ----------------------------------------------------------------------------
137 // these functions are deprecated, use wxString methods instead!
138 #if WXWIN_COMPATIBILITY_2_4
140 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxFloatToStringStr
;
141 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxDoubleToStringStr
;
143 wxDEPRECATED( WXDLLIMPEXP_BASE
void StringToFloat(const wxChar
*s
, float *number
) );
144 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
) );
145 wxDEPRECATED( WXDLLIMPEXP_BASE
void StringToDouble(const wxChar
*s
, double *number
) );
146 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
) );
147 wxDEPRECATED( WXDLLIMPEXP_BASE
void StringToInt(const wxChar
*s
, int *number
) );
148 wxDEPRECATED( WXDLLIMPEXP_BASE
void StringToLong(const wxChar
*s
, long *number
) );
149 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* IntToString(int number
) );
150 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* LongToString(long number
) );
152 #endif // WXWIN_COMPATIBILITY_2_4
154 // Convert 2-digit hex number to decimal
155 WXDLLIMPEXP_BASE
int wxHexToDec(const wxString
& buf
);
157 // Convert decimal integer to 2-character hex string
158 WXDLLIMPEXP_BASE
void wxDecToHex(int dec
, wxChar
*buf
);
159 WXDLLIMPEXP_BASE wxString
wxDecToHex(int dec
);
161 // ----------------------------------------------------------------------------
162 // Process management
163 // ----------------------------------------------------------------------------
165 // NB: for backwards compatibility reasons the values of wxEXEC_[A]SYNC *must*
166 // be 0 and 1, don't change!
170 // execute the process asynchronously
173 // execute it synchronously, i.e. wait until it finishes
176 // under Windows, don't hide the child even if it's IO is redirected (this
177 // is done by default)
180 // under Unix, if the process is the group leader then passing wxKILL_CHILDREN to wxKill
181 // kills all children as well as pid
182 wxEXEC_MAKE_GROUP_LEADER
= 4,
184 // by default synchronous execution disables all program windows to avoid
185 // that the user interacts with the program while the child process is
186 // running, you can use this flag to prevent this from happening
190 // Execute another program.
192 // If flags contain wxEXEC_SYNC, return -1 on failure and the exit code of the
193 // process if everything was ok. Otherwise (i.e. if wxEXEC_ASYNC), return 0 on
194 // failure and the PID of the launched process if ok.
195 WXDLLIMPEXP_BASE
long wxExecute(wxChar
**argv
, int flags
= wxEXEC_ASYNC
,
196 wxProcess
*process
= (wxProcess
*) NULL
);
197 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
, int flags
= wxEXEC_ASYNC
,
198 wxProcess
*process
= (wxProcess
*) NULL
);
200 // execute the command capturing its output into an array line by line, this is
201 // always synchronous
202 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
203 wxArrayString
& output
,
206 // also capture stderr (also synchronous)
207 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
208 wxArrayString
& output
,
209 wxArrayString
& error
,
213 // ask a DDE server to execute the DDE request with given parameters
214 WXDLLIMPEXP_BASE
bool wxExecuteDDE(const wxString
& ddeServer
,
215 const wxString
& ddeTopic
,
216 const wxString
& ddeCommand
);
221 wxSIGNONE
= 0, // verify if the process exists under Unix
228 wxSIGIOT
= wxSIGABRT
, // another name
239 // further signals are different in meaning between different Unix systems
244 wxKILL_OK
, // no error
245 wxKILL_BAD_SIGNAL
, // no such signal
246 wxKILL_ACCESS_DENIED
, // permission denied
247 wxKILL_NO_PROCESS
, // no such process
248 wxKILL_ERROR
// another, unspecified error
253 wxKILL_NOCHILDREN
= 0, // don't kill children
254 wxKILL_CHILDREN
= 1 // kill children
259 wxSHUTDOWN_POWEROFF
, // power off the computer
260 wxSHUTDOWN_REBOOT
// shutdown and reboot
263 // Shutdown or reboot the PC
264 WXDLLIMPEXP_BASE
bool wxShutdown(wxShutdownFlags wFlags
);
273 WXDLLIMPEXP_BASE wxPowerType
wxGetPowerType();
277 wxBATTERY_NORMAL_STATE
, // system is fully usable
278 wxBATTERY_LOW_STATE
, // start to worry
279 wxBATTERY_CRITICAL_STATE
, // save quickly
280 wxBATTERY_SHUTDOWN_STATE
, // too late
281 wxBATTERY_UNKNOWN_STATE
284 WXDLLIMPEXP_BASE wxBatteryState
wxGetBatteryState();
286 // send the given signal to the process (only NONE and KILL are supported under
287 // Windows, all others mean TERM), return 0 if ok and -1 on error
289 // return detailed error in rc if not NULL
290 WXDLLIMPEXP_BASE
int wxKill(long pid
,
291 wxSignal sig
= wxSIGTERM
,
292 wxKillError
*rc
= NULL
,
293 int flags
= wxKILL_NOCHILDREN
);
295 // Execute a command in an interactive shell window (always synchronously)
296 // If no command then just the shell
297 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
= wxEmptyString
);
299 // As wxShell(), but must give a (non interactive) command and its output will
300 // be returned in output array
301 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
, wxArrayString
& output
);
303 // Sleep for nSecs seconds
304 WXDLLIMPEXP_BASE
void wxSleep(int nSecs
);
306 // Sleep for a given amount of milliseconds
307 WXDLLIMPEXP_BASE
void wxMilliSleep(unsigned long milliseconds
);
309 // Sleep for a given amount of microseconds
310 WXDLLIMPEXP_BASE
void wxMicroSleep(unsigned long microseconds
);
312 // Sleep for a given amount of milliseconds (old, bad name), use wxMilliSleep
313 wxDEPRECATED( WXDLLIMPEXP_BASE
void wxUsleep(unsigned long milliseconds
) );
315 // Get the process id of the current process
316 WXDLLIMPEXP_BASE
unsigned long wxGetProcessId();
318 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
319 WXDLLIMPEXP_BASE wxMemorySize
wxGetFreeMemory();
321 #if wxUSE_ON_FATAL_EXCEPTION
323 // should wxApp::OnFatalException() be called?
324 WXDLLIMPEXP_BASE
bool wxHandleFatalExceptions(bool doit
= true);
326 #endif // wxUSE_ON_FATAL_EXCEPTION
328 #if wxABI_VERSION >= 20601
330 // flags for wxLaunchDefaultBrowser
333 wxBROWSER_NEW_WINDOW
= 1
336 // Launch url in the user's default internet browser
337 WXDLLIMPEXP_BASE
bool wxLaunchDefaultBrowser(const wxString
& url
, int flags
= 0);
340 // ----------------------------------------------------------------------------
341 // Environment variables
342 // ----------------------------------------------------------------------------
344 // returns true if variable exists (value may be NULL if you just want to check
346 WXDLLIMPEXP_BASE
bool wxGetEnv(const wxString
& var
, wxString
*value
);
348 // set the env var name to the given value, return true on success
349 WXDLLIMPEXP_BASE
bool wxSetEnv(const wxString
& var
, const wxChar
*value
);
351 // remove the env var from environment
352 inline bool wxUnsetEnv(const wxString
& var
) { return wxSetEnv(var
, NULL
); }
354 // ----------------------------------------------------------------------------
355 // Network and username functions.
356 // ----------------------------------------------------------------------------
358 // NB: "char *" functions are deprecated, use wxString ones!
361 WXDLLIMPEXP_BASE
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
362 WXDLLIMPEXP_BASE wxString
wxGetEmailAddress();
365 WXDLLIMPEXP_BASE
bool wxGetHostName(wxChar
*buf
, int maxSize
);
366 WXDLLIMPEXP_BASE wxString
wxGetHostName();
369 WXDLLIMPEXP_BASE wxString
wxGetFullHostName();
370 WXDLLIMPEXP_BASE
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
372 // Get user ID e.g. jacs (this is known as login name under Unix)
373 WXDLLIMPEXP_BASE
bool wxGetUserId(wxChar
*buf
, int maxSize
);
374 WXDLLIMPEXP_BASE wxString
wxGetUserId();
376 // Get user name e.g. Julian Smart
377 WXDLLIMPEXP_BASE
bool wxGetUserName(wxChar
*buf
, int maxSize
);
378 WXDLLIMPEXP_BASE wxString
wxGetUserName();
380 // Get current Home dir and copy to dest (returns pstr->c_str())
381 WXDLLIMPEXP_BASE wxString
wxGetHomeDir();
382 WXDLLIMPEXP_BASE
const wxChar
* wxGetHomeDir(wxString
*pstr
);
384 // Get the user's home dir (caller must copy --- volatile)
385 // returns NULL is no HOME dir is known
386 #if defined(__UNIX__) && wxUSE_UNICODE
387 WXDLLIMPEXP_BASE
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
389 WXDLLIMPEXP_BASE wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
392 // get number of total/free bytes on the disk where path belongs
393 WXDLLIMPEXP_BASE
bool wxGetDiskSpace(const wxString
& path
,
394 wxLongLong
*pTotal
= NULL
,
395 wxLongLong
*pFree
= NULL
);
397 #if wxUSE_GUI // GUI only things from now on
399 // ----------------------------------------------------------------------------
400 // Menu accelerators related things
401 // ----------------------------------------------------------------------------
403 WXDLLEXPORT wxChar
* wxStripMenuCodes(const wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
404 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
407 class WXDLLEXPORT wxAcceleratorEntry
;
408 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
409 #endif // wxUSE_ACCEL
411 // ----------------------------------------------------------------------------
413 // ----------------------------------------------------------------------------
415 // Returns menu item id or wxNOT_FOUND if none.
416 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
418 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
419 // is always present but may be less reliable than a native version.
420 WXDLLEXPORT wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
);
421 WXDLLEXPORT wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
);
423 // NB: this function is obsolete, use wxWindow::FindWindowByLabel() instead
425 // Find the window/widget with the given title or label.
426 // Pass a parent to begin the search from, or NULL to look through
428 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
430 // NB: this function is obsolete, use wxWindow::FindWindowByName() instead
432 // Find window by name, and if that fails, by label.
433 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
435 // ----------------------------------------------------------------------------
436 // Message/event queue helpers
437 // ----------------------------------------------------------------------------
439 // Yield to other apps/messages and disable user input
440 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
, bool onlyIfNeeded
= false);
442 // Enable or disable input to all top level windows
443 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= true);
445 // Check whether this window wants to process messages, e.g. Stop button
446 // in long calculations.
447 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
449 // Consume all events until no more left
450 WXDLLEXPORT
void wxFlushEvents();
452 // a class which disables all windows (except, may be, thegiven one) in its
453 // ctor and enables them back in its dtor
454 class WXDLLEXPORT wxWindowDisabler
457 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
461 wxWindowList
*m_winDisabled
;
463 DECLARE_NO_COPY_CLASS(wxWindowDisabler
)
466 // ----------------------------------------------------------------------------
468 // ----------------------------------------------------------------------------
470 // Set the cursor to the busy cursor for all windows
471 class WXDLLEXPORT wxCursor
;
472 extern WXDLLEXPORT_DATA(wxCursor
*) wxHOURGLASS_CURSOR
;
473 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
475 // Restore cursor to normal
476 WXDLLEXPORT
void wxEndBusyCursor();
478 // true if we're between the above two calls
479 WXDLLEXPORT
bool wxIsBusy();
481 // Convenience class so we can just create a wxBusyCursor object on the stack
482 class WXDLLEXPORT wxBusyCursor
485 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
486 { wxBeginBusyCursor(cursor
); }
488 { wxEndBusyCursor(); }
490 // FIXME: These two methods are currently only implemented (and needed?)
491 // in wxGTK. BusyCursor handling should probably be moved to
492 // common code since the wxGTK and wxMSW implementations are very
493 // similar except for wxMSW using HCURSOR directly instead of
495 static const wxCursor
&GetStoredCursor();
496 static const wxCursor
GetBusyCursor();
500 // ----------------------------------------------------------------------------
501 // Reading and writing resources (eg WIN.INI, .Xdefaults)
502 // ----------------------------------------------------------------------------
505 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
506 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
507 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
508 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
510 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
511 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
512 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
513 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
514 #endif // wxUSE_RESOURCES
516 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
518 // MSW only: get user-defined resource from the .res file.
519 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
521 extern WXDLLEXPORT
const wxChar
* wxUserResourceStr
;
522 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
525 // ----------------------------------------------------------------------------
526 // Display and colorss (X only)
527 // ----------------------------------------------------------------------------
530 void *wxGetDisplay();
534 WXDLLIMPEXP_CORE WXDisplay
*wxGetDisplay();
535 WXDLLIMPEXP_CORE
bool wxSetDisplay(const wxString
& display_name
);
536 WXDLLIMPEXP_CORE wxString
wxGetDisplayName();
541 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
542 // The resulting warnings are switched off here
543 #pragma message disable nosimpint
545 // #include <X11/Xlib.h>
547 #pragma message enable nosimpint
554 // ----------------------------------------------------------------------------
555 // wxYield(): these functions are obsolete, please use wxApp methods instead!
556 // ----------------------------------------------------------------------------
558 // Yield to other apps/messages
559 WXDLLIMPEXP_BASE
bool wxYield();
561 // Like wxYield, but fails silently if the yield is recursive.
562 WXDLLIMPEXP_BASE
bool wxYieldIfNeeded();
564 // ----------------------------------------------------------------------------
565 // Error message functions used by wxWidgets (deprecated, use wxLog)
566 // ----------------------------------------------------------------------------
568 #if WXWIN_COMPATIBILITY_2_2
570 // Format a message on the standard error (UNIX) or the debugging
572 wxDEPRECATED( WXDLLIMPEXP_BASE
void wxDebugMsg(const wxChar
*fmt
...) ATTRIBUTE_PRINTF_1
);
574 // Non-fatal error (continues)
575 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxInternalErrorStr
;
576 wxDEPRECATED( WXDLLIMPEXP_BASE
void wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
) );
578 // Fatal error (exits)
579 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxFatalErrorStr
;
580 wxDEPRECATED( WXDLLIMPEXP_BASE
void wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
) );
582 #endif // WXWIN_COMPATIBILITY_2_2