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
);
118 // wxMouseState is used to hold information about button and modifier state
119 // and is what is returned from wxGetMouseState.
120 class WXDLLEXPORT wxMouseState
125 m_leftDown(false), m_middleDown(false), m_rightDown(false),
126 m_controlDown(false), m_shiftDown(false), m_altDown(false),
130 wxCoord
GetX() { return m_x
; }
131 wxCoord
GetY() { return m_y
; }
133 bool LeftDown() { return m_leftDown
; }
134 bool MiddleDown() { return m_middleDown
; }
135 bool RightDown() { return m_rightDown
; }
137 bool ControlDown() { return m_controlDown
; }
138 bool ShiftDown() { return m_shiftDown
; }
139 bool AltDown() { return m_altDown
; }
140 bool MetaDown() { return m_metaDown
; }
143 #if defined(__WXMAC__) || defined(__WXCOCOA__)
146 return ControlDown();
150 void SetX(wxCoord x
) { m_x
= x
; }
151 void SetY(wxCoord y
) { m_y
= y
; }
153 void SetLeftDown(bool down
) { m_leftDown
= down
; }
154 void SetMiddleDown(bool down
) { m_middleDown
= down
; }
155 void SetRightDown(bool down
) { m_rightDown
= down
; }
157 void SetControlDown(bool down
) { m_controlDown
= down
; }
158 void SetShiftDown(bool down
) { m_shiftDown
= down
; }
159 void SetAltDown(bool down
) { m_altDown
= down
; }
160 void SetMetaDown(bool down
) { m_metaDown
= down
; }
177 // Returns the current state of the mouse position, buttons and modifers
178 WXDLLEXPORT wxMouseState
wxGetMouseState();
181 // ----------------------------------------------------------------------------
182 // Window ID management
183 // ----------------------------------------------------------------------------
185 // Generate a unique ID
186 WXDLLEXPORT
long wxNewId();
188 // Ensure subsequent IDs don't clash with this one
189 WXDLLEXPORT
void wxRegisterId(long id
);
191 // Return the current ID
192 WXDLLEXPORT
long wxGetCurrentId();
196 // ----------------------------------------------------------------------------
197 // Various conversions
198 // ----------------------------------------------------------------------------
200 // these functions are deprecated, use wxString methods instead!
201 #if WXWIN_COMPATIBILITY_2_4
203 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxFloatToStringStr
;
204 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxDoubleToStringStr
;
206 wxDEPRECATED( WXDLLIMPEXP_BASE
void StringToFloat(const wxChar
*s
, float *number
) );
207 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
) );
208 wxDEPRECATED( WXDLLIMPEXP_BASE
void StringToDouble(const wxChar
*s
, double *number
) );
209 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
) );
210 wxDEPRECATED( WXDLLIMPEXP_BASE
void StringToInt(const wxChar
*s
, int *number
) );
211 wxDEPRECATED( WXDLLIMPEXP_BASE
void StringToLong(const wxChar
*s
, long *number
) );
212 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* IntToString(int number
) );
213 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* LongToString(long number
) );
215 #endif // WXWIN_COMPATIBILITY_2_4
217 // Convert 2-digit hex number to decimal
218 WXDLLIMPEXP_BASE
int wxHexToDec(const wxString
& buf
);
220 // Convert decimal integer to 2-character hex string
221 WXDLLIMPEXP_BASE
void wxDecToHex(int dec
, wxChar
*buf
);
222 WXDLLIMPEXP_BASE wxString
wxDecToHex(int dec
);
224 // ----------------------------------------------------------------------------
225 // Process management
226 // ----------------------------------------------------------------------------
228 // NB: for backwards compatibility reasons the values of wxEXEC_[A]SYNC *must*
229 // be 0 and 1, don't change!
233 // execute the process asynchronously
236 // execute it synchronously, i.e. wait until it finishes
239 // under Windows, don't hide the child even if it's IO is redirected (this
240 // is done by default)
243 // under Unix, if the process is the group leader then passing wxKILL_CHILDREN to wxKill
244 // kills all children as well as pid
245 wxEXEC_MAKE_GROUP_LEADER
= 4,
247 // by default synchronous execution disables all program windows to avoid
248 // that the user interacts with the program while the child process is
249 // running, you can use this flag to prevent this from happening
253 // Execute another program.
255 // If flags contain wxEXEC_SYNC, return -1 on failure and the exit code of the
256 // process if everything was ok. Otherwise (i.e. if wxEXEC_ASYNC), return 0 on
257 // failure and the PID of the launched process if ok.
258 WXDLLIMPEXP_BASE
long wxExecute(wxChar
**argv
, int flags
= wxEXEC_ASYNC
,
259 wxProcess
*process
= (wxProcess
*) NULL
);
260 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
, int flags
= wxEXEC_ASYNC
,
261 wxProcess
*process
= (wxProcess
*) NULL
);
263 // execute the command capturing its output into an array line by line, this is
264 // always synchronous
265 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
266 wxArrayString
& output
,
269 // also capture stderr (also synchronous)
270 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
271 wxArrayString
& output
,
272 wxArrayString
& error
,
275 #if defined(__WXMSW__) && wxUSE_IPC
276 // ask a DDE server to execute the DDE request with given parameters
277 WXDLLIMPEXP_BASE
bool wxExecuteDDE(const wxString
& ddeServer
,
278 const wxString
& ddeTopic
,
279 const wxString
& ddeCommand
);
280 #endif // __WXMSW__ && wxUSE_IPC
284 wxSIGNONE
= 0, // verify if the process exists under Unix
291 wxSIGIOT
= wxSIGABRT
, // another name
302 // further signals are different in meaning between different Unix systems
307 wxKILL_OK
, // no error
308 wxKILL_BAD_SIGNAL
, // no such signal
309 wxKILL_ACCESS_DENIED
, // permission denied
310 wxKILL_NO_PROCESS
, // no such process
311 wxKILL_ERROR
// another, unspecified error
316 wxKILL_NOCHILDREN
= 0, // don't kill children
317 wxKILL_CHILDREN
= 1 // kill children
322 wxSHUTDOWN_POWEROFF
, // power off the computer
323 wxSHUTDOWN_REBOOT
// shutdown and reboot
326 // Shutdown or reboot the PC
327 WXDLLIMPEXP_BASE
bool wxShutdown(wxShutdownFlags wFlags
);
336 WXDLLIMPEXP_BASE wxPowerType
wxGetPowerType();
340 wxBATTERY_NORMAL_STATE
, // system is fully usable
341 wxBATTERY_LOW_STATE
, // start to worry
342 wxBATTERY_CRITICAL_STATE
, // save quickly
343 wxBATTERY_SHUTDOWN_STATE
, // too late
344 wxBATTERY_UNKNOWN_STATE
347 WXDLLIMPEXP_BASE wxBatteryState
wxGetBatteryState();
349 // send the given signal to the process (only NONE and KILL are supported under
350 // Windows, all others mean TERM), return 0 if ok and -1 on error
352 // return detailed error in rc if not NULL
353 WXDLLIMPEXP_BASE
int wxKill(long pid
,
354 wxSignal sig
= wxSIGTERM
,
355 wxKillError
*rc
= NULL
,
356 int flags
= wxKILL_NOCHILDREN
);
358 // Execute a command in an interactive shell window (always synchronously)
359 // If no command then just the shell
360 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
= wxEmptyString
);
362 // As wxShell(), but must give a (non interactive) command and its output will
363 // be returned in output array
364 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
, wxArrayString
& output
);
366 // Sleep for nSecs seconds
367 WXDLLIMPEXP_BASE
void wxSleep(int nSecs
);
369 // Sleep for a given amount of milliseconds
370 WXDLLIMPEXP_BASE
void wxMilliSleep(unsigned long milliseconds
);
372 // Sleep for a given amount of microseconds
373 WXDLLIMPEXP_BASE
void wxMicroSleep(unsigned long microseconds
);
375 // Sleep for a given amount of milliseconds (old, bad name), use wxMilliSleep
376 wxDEPRECATED( WXDLLIMPEXP_BASE
void wxUsleep(unsigned long milliseconds
) );
378 // Get the process id of the current process
379 WXDLLIMPEXP_BASE
unsigned long wxGetProcessId();
381 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
382 WXDLLIMPEXP_BASE wxMemorySize
wxGetFreeMemory();
384 #if wxUSE_ON_FATAL_EXCEPTION
386 // should wxApp::OnFatalException() be called?
387 WXDLLIMPEXP_BASE
bool wxHandleFatalExceptions(bool doit
= true);
389 #endif // wxUSE_ON_FATAL_EXCEPTION
391 // flags for wxLaunchDefaultBrowser
394 wxBROWSER_NEW_WINDOW
= 1
397 // Launch url in the user's default internet browser
398 WXDLLIMPEXP_BASE
bool wxLaunchDefaultBrowser(const wxString
& url
, int flags
= 0);
400 // ----------------------------------------------------------------------------
401 // Environment variables
402 // ----------------------------------------------------------------------------
404 // returns true if variable exists (value may be NULL if you just want to check
406 WXDLLIMPEXP_BASE
bool wxGetEnv(const wxString
& var
, wxString
*value
);
408 // set the env var name to the given value, return true on success
409 WXDLLIMPEXP_BASE
bool wxSetEnv(const wxString
& var
, const wxChar
*value
);
411 // remove the env var from environment
412 inline bool wxUnsetEnv(const wxString
& var
) { return wxSetEnv(var
, NULL
); }
414 // ----------------------------------------------------------------------------
415 // Network and username functions.
416 // ----------------------------------------------------------------------------
418 // NB: "char *" functions are deprecated, use wxString ones!
421 WXDLLIMPEXP_BASE
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
422 WXDLLIMPEXP_BASE wxString
wxGetEmailAddress();
425 WXDLLIMPEXP_BASE
bool wxGetHostName(wxChar
*buf
, int maxSize
);
426 WXDLLIMPEXP_BASE wxString
wxGetHostName();
429 WXDLLIMPEXP_BASE wxString
wxGetFullHostName();
430 WXDLLIMPEXP_BASE
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
432 // Get user ID e.g. jacs (this is known as login name under Unix)
433 WXDLLIMPEXP_BASE
bool wxGetUserId(wxChar
*buf
, int maxSize
);
434 WXDLLIMPEXP_BASE wxString
wxGetUserId();
436 // Get user name e.g. Julian Smart
437 WXDLLIMPEXP_BASE
bool wxGetUserName(wxChar
*buf
, int maxSize
);
438 WXDLLIMPEXP_BASE wxString
wxGetUserName();
440 // Get current Home dir and copy to dest (returns pstr->c_str())
441 WXDLLIMPEXP_BASE wxString
wxGetHomeDir();
442 WXDLLIMPEXP_BASE
const wxChar
* wxGetHomeDir(wxString
*pstr
);
444 // Get the user's home dir (caller must copy --- volatile)
445 // returns NULL is no HOME dir is known
446 #if defined(__UNIX__) && wxUSE_UNICODE
447 WXDLLIMPEXP_BASE
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
449 WXDLLIMPEXP_BASE wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
452 // get number of total/free bytes on the disk where path belongs
453 WXDLLIMPEXP_BASE
bool wxGetDiskSpace(const wxString
& path
,
454 wxLongLong
*pTotal
= NULL
,
455 wxLongLong
*pFree
= NULL
);
457 #if wxUSE_GUI // GUI only things from now on
459 // ----------------------------------------------------------------------------
460 // Menu accelerators related things
461 // ----------------------------------------------------------------------------
463 WXDLLEXPORT wxChar
* wxStripMenuCodes(const wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
464 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
467 class WXDLLEXPORT wxAcceleratorEntry
;
468 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
469 #endif // wxUSE_ACCEL
471 // ----------------------------------------------------------------------------
473 // ----------------------------------------------------------------------------
475 // Returns menu item id or wxNOT_FOUND if none.
476 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
478 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
479 // is always present but may be less reliable than a native version.
480 WXDLLEXPORT wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
);
481 WXDLLEXPORT wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
);
483 // NB: this function is obsolete, use wxWindow::FindWindowByLabel() instead
485 // Find the window/widget with the given title or label.
486 // Pass a parent to begin the search from, or NULL to look through
488 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
490 // NB: this function is obsolete, use wxWindow::FindWindowByName() instead
492 // Find window by name, and if that fails, by label.
493 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
495 // ----------------------------------------------------------------------------
496 // Message/event queue helpers
497 // ----------------------------------------------------------------------------
499 // Yield to other apps/messages and disable user input
500 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
, bool onlyIfNeeded
= false);
502 // Enable or disable input to all top level windows
503 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= true);
505 // Check whether this window wants to process messages, e.g. Stop button
506 // in long calculations.
507 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
509 // Consume all events until no more left
510 WXDLLEXPORT
void wxFlushEvents();
512 // a class which disables all windows (except, may be, thegiven one) in its
513 // ctor and enables them back in its dtor
514 class WXDLLEXPORT wxWindowDisabler
517 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
521 wxWindowList
*m_winDisabled
;
523 DECLARE_NO_COPY_CLASS(wxWindowDisabler
)
526 // ----------------------------------------------------------------------------
528 // ----------------------------------------------------------------------------
530 // Set the cursor to the busy cursor for all windows
531 class WXDLLEXPORT wxCursor
;
532 extern WXDLLEXPORT_DATA(wxCursor
*) wxHOURGLASS_CURSOR
;
533 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
535 // Restore cursor to normal
536 WXDLLEXPORT
void wxEndBusyCursor();
538 // true if we're between the above two calls
539 WXDLLEXPORT
bool wxIsBusy();
541 // Convenience class so we can just create a wxBusyCursor object on the stack
542 class WXDLLEXPORT wxBusyCursor
545 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
546 { wxBeginBusyCursor(cursor
); }
548 { wxEndBusyCursor(); }
550 // FIXME: These two methods are currently only implemented (and needed?)
551 // in wxGTK. BusyCursor handling should probably be moved to
552 // common code since the wxGTK and wxMSW implementations are very
553 // similar except for wxMSW using HCURSOR directly instead of
555 static const wxCursor
&GetStoredCursor();
556 static const wxCursor
GetBusyCursor();
560 // ----------------------------------------------------------------------------
561 // Reading and writing resources (eg WIN.INI, .Xdefaults)
562 // ----------------------------------------------------------------------------
565 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
566 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
567 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
568 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
570 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
571 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
572 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
573 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
574 #endif // wxUSE_RESOURCES
576 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
578 // MSW only: get user-defined resource from the .res file.
579 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
581 extern WXDLLEXPORT
const wxChar
* wxUserResourceStr
;
582 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
585 // ----------------------------------------------------------------------------
586 // Display and colorss (X only)
587 // ----------------------------------------------------------------------------
590 void *wxGetDisplay();
594 WXDLLIMPEXP_CORE WXDisplay
*wxGetDisplay();
595 WXDLLIMPEXP_CORE
bool wxSetDisplay(const wxString
& display_name
);
596 WXDLLIMPEXP_CORE wxString
wxGetDisplayName();
601 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
602 // The resulting warnings are switched off here
603 #pragma message disable nosimpint
605 // #include <X11/Xlib.h>
607 #pragma message enable nosimpint
614 // ----------------------------------------------------------------------------
615 // wxYield(): these functions are obsolete, please use wxApp methods instead!
616 // ----------------------------------------------------------------------------
618 // Yield to other apps/messages
619 WXDLLIMPEXP_BASE
bool wxYield();
621 // Like wxYield, but fails silently if the yield is recursive.
622 WXDLLIMPEXP_BASE
bool wxYieldIfNeeded();
624 // ----------------------------------------------------------------------------
625 // Error message functions used by wxWidgets (deprecated, use wxLog)
626 // ----------------------------------------------------------------------------