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 #include "wx/gdicmn.h"
26 class WXDLLIMPEXP_BASE wxArrayString
;
27 class WXDLLIMPEXP_BASE wxArrayInt
;
29 // need this for wxGetDiskSpace() as we can't, unfortunately, forward declare
31 #include "wx/longlong.h"
33 // need for wxOperatingSystemId
34 #include "wx/platinfo.h"
45 // ----------------------------------------------------------------------------
46 // Forward declaration
47 // ----------------------------------------------------------------------------
49 class WXDLLIMPEXP_CORE wxProcess
;
50 class WXDLLIMPEXP_CORE wxFrame
;
51 class WXDLLIMPEXP_CORE wxWindow
;
52 class WXDLLIMPEXP_CORE wxWindowList
;
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
59 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
60 #define wxClip(a,b,c) (((a) < (b)) ? (b) : (((a) > (c)) ? (c) : (a)))
62 // wxGetFreeMemory can return huge amount of memory on 32-bit platforms as well
63 // so to always use long long for its result type on all platforms which
66 typedef wxLongLong wxMemorySize
;
68 typedef long wxMemorySize
;
71 // ----------------------------------------------------------------------------
72 // String functions (deprecated, use wxString)
73 // ----------------------------------------------------------------------------
75 // A shorter way of using strcmp
76 #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
78 // ----------------------------------------------------------------------------
79 // Miscellaneous functions
80 // ----------------------------------------------------------------------------
83 #if !defined __EMX__ && \
84 (defined __WXMOTIF__ || defined __WXGTK__ || defined __WXX11__)
85 WXDLLIMPEXP_CORE
void wxBell();
87 WXDLLIMPEXP_BASE
void wxBell();
90 // Get OS description as a user-readable string
91 WXDLLIMPEXP_BASE wxString
wxGetOsDescription();
94 WXDLLIMPEXP_BASE wxOperatingSystemId
wxGetOsVersion(int *majorVsn
= (int *) NULL
,
95 int *minorVsn
= (int *) NULL
);
97 // Get platform endianness
98 WXDLLIMPEXP_BASE
bool wxIsPlatformLittleEndian();
100 // Get platform architecture
101 WXDLLIMPEXP_BASE
bool wxIsPlatform64Bit();
103 // Return a string with the current date/time
104 WXDLLIMPEXP_BASE wxString
wxNow();
106 // Return path where wxWidgets is installed (mostly useful in Unices)
107 WXDLLIMPEXP_BASE
const wxChar
*wxGetInstallPrefix();
108 // Return path to wxWin data (/usr/share/wx/%{version}) (Unices)
109 WXDLLIMPEXP_BASE wxString
wxGetDataDir();
112 * Class to make it easier to specify platform-dependent values
115 * long val = wxPlatform::If(wxMac, 1).ElseIf(wxGTK, 2).ElseIf(stPDA, 5).Else(3);
116 * wxString strVal = wxPlatform::If(wxMac, wxT("Mac")).ElseIf(wxMSW, wxT("MSW")).Else(wxT("Other"));
118 * A custom platform symbol:
122 * wxPlatform::AddPlatform(stPDA);
125 * long windowStyle = wxCAPTION | (long) wxPlatform::IfNot(stPDA, wxRESIZE_BORDER);
129 class WXDLLIMPEXP_BASE wxPlatform
132 wxPlatform() { Init(); }
133 wxPlatform(const wxPlatform
& platform
) { Copy(platform
); }
134 void operator = (const wxPlatform
& platform
) { Copy(platform
); }
135 void Copy(const wxPlatform
& platform
);
137 // Specify an optional default value
138 wxPlatform(int defValue
) { Init(); m_longValue
= (long)defValue
; }
139 wxPlatform(long defValue
) { Init(); m_longValue
= defValue
; }
140 wxPlatform(const wxString
& defValue
) { Init(); m_stringValue
= defValue
; }
141 wxPlatform(double defValue
) { Init(); m_doubleValue
= defValue
; }
143 static wxPlatform
If(int platform
, long value
);
144 static wxPlatform
IfNot(int platform
, long value
);
145 wxPlatform
& ElseIf(int platform
, long value
);
146 wxPlatform
& ElseIfNot(int platform
, long value
);
147 wxPlatform
& Else(long value
);
149 static wxPlatform
If(int platform
, int value
) { return If(platform
, (long)value
); }
150 static wxPlatform
IfNot(int platform
, int value
) { return IfNot(platform
, (long)value
); }
151 wxPlatform
& ElseIf(int platform
, int value
) { return ElseIf(platform
, (long) value
); }
152 wxPlatform
& ElseIfNot(int platform
, int value
) { return ElseIfNot(platform
, (long) value
); }
153 wxPlatform
& Else(int value
) { return Else((long) value
); }
155 static wxPlatform
If(int platform
, double value
);
156 static wxPlatform
IfNot(int platform
, double value
);
157 wxPlatform
& ElseIf(int platform
, double value
);
158 wxPlatform
& ElseIfNot(int platform
, double value
);
159 wxPlatform
& Else(double value
);
161 static wxPlatform
If(int platform
, const wxString
& value
);
162 static wxPlatform
IfNot(int platform
, const wxString
& value
);
163 wxPlatform
& ElseIf(int platform
, const wxString
& value
);
164 wxPlatform
& ElseIfNot(int platform
, const wxString
& value
);
165 wxPlatform
& Else(const wxString
& value
);
167 long GetInteger() const { return m_longValue
; }
168 const wxString
& GetString() const { return m_stringValue
; }
169 double GetDouble() const { return m_doubleValue
; }
171 operator int() const { return (int) GetInteger(); }
172 operator long() const { return GetInteger(); }
173 operator double() const { return GetDouble(); }
174 operator const wxString
&() const { return GetString(); }
176 static void AddPlatform(int platform
);
177 static bool Is(int platform
);
178 static void ClearPlatforms();
182 void Init() { m_longValue
= 0; m_doubleValue
= 0.0; }
185 double m_doubleValue
;
186 wxString m_stringValue
;
187 static wxArrayInt
* sm_customPlatforms
;
190 /// Function for testing current platform
191 inline bool wxPlatformIs(int platform
) { return wxPlatform::Is(platform
); }
195 // Get the state of a key (true if pressed, false if not)
196 // This is generally most useful getting the state of
197 // the modifier or toggle keys.
198 WXDLLEXPORT
bool wxGetKeyState(wxKeyCode key
);
201 // Don't synthesize KeyUp events holding down a key and producing
202 // KeyDown events with autorepeat. On by default and always on
204 WXDLLEXPORT
bool wxSetDetectableAutoRepeat( bool flag
);
207 // wxMouseState is used to hold information about button and modifier state
208 // and is what is returned from wxGetMouseState.
209 class WXDLLEXPORT wxMouseState
214 m_leftDown(false), m_middleDown(false), m_rightDown(false),
215 m_controlDown(false), m_shiftDown(false), m_altDown(false),
219 wxCoord
GetX() { return m_x
; }
220 wxCoord
GetY() { return m_y
; }
222 bool LeftDown() { return m_leftDown
; }
223 bool MiddleDown() { return m_middleDown
; }
224 bool RightDown() { return m_rightDown
; }
226 bool ControlDown() { return m_controlDown
; }
227 bool ShiftDown() { return m_shiftDown
; }
228 bool AltDown() { return m_altDown
; }
229 bool MetaDown() { return m_metaDown
; }
232 #if defined(__WXMAC__) || defined(__WXCOCOA__)
235 return ControlDown();
239 void SetX(wxCoord x
) { m_x
= x
; }
240 void SetY(wxCoord y
) { m_y
= y
; }
242 void SetLeftDown(bool down
) { m_leftDown
= down
; }
243 void SetMiddleDown(bool down
) { m_middleDown
= down
; }
244 void SetRightDown(bool down
) { m_rightDown
= down
; }
246 void SetControlDown(bool down
) { m_controlDown
= down
; }
247 void SetShiftDown(bool down
) { m_shiftDown
= down
; }
248 void SetAltDown(bool down
) { m_altDown
= down
; }
249 void SetMetaDown(bool down
) { m_metaDown
= down
; }
256 bool m_middleDown
: 1;
257 bool m_rightDown
: 1;
259 bool m_controlDown
: 1;
260 bool m_shiftDown
: 1;
266 // Returns the current state of the mouse position, buttons and modifers
267 WXDLLEXPORT wxMouseState
wxGetMouseState();
270 // ----------------------------------------------------------------------------
271 // Window ID management
272 // ----------------------------------------------------------------------------
274 // Generate a unique ID
275 WXDLLEXPORT
long wxNewId();
277 // Ensure subsequent IDs don't clash with this one
278 WXDLLEXPORT
void wxRegisterId(long id
);
280 // Return the current ID
281 WXDLLEXPORT
long wxGetCurrentId();
285 // ----------------------------------------------------------------------------
286 // Various conversions
287 // ----------------------------------------------------------------------------
289 // Convert 2-digit hex number to decimal
290 WXDLLIMPEXP_BASE
int wxHexToDec(const wxString
& buf
);
292 // Convert decimal integer to 2-character hex string
293 WXDLLIMPEXP_BASE
void wxDecToHex(int dec
, wxChar
*buf
);
294 WXDLLIMPEXP_BASE wxString
wxDecToHex(int dec
);
296 // ----------------------------------------------------------------------------
297 // Process management
298 // ----------------------------------------------------------------------------
300 // NB: for backwards compatibility reasons the values of wxEXEC_[A]SYNC *must*
301 // be 0 and 1, don't change!
305 // execute the process asynchronously
308 // execute it synchronously, i.e. wait until it finishes
311 // under Windows, don't hide the child even if it's IO is redirected (this
312 // is done by default)
315 // under Unix, if the process is the group leader then passing wxKILL_CHILDREN to wxKill
316 // kills all children as well as pid
317 wxEXEC_MAKE_GROUP_LEADER
= 4,
319 // by default synchronous execution disables all program windows to avoid
320 // that the user interacts with the program while the child process is
321 // running, you can use this flag to prevent this from happening
322 wxEXEC_NODISABLE
= 8,
324 // by default, the event loop is run while waiting for synchronous execution
325 // to complete and this flag can be used to simply block the main process
326 // until the child process finishes
327 wxEXEC_NOEVENTS
= 16,
329 // convenient synonym for flags given system()-like behaviour
330 wxEXEC_BLOCK
= wxEXEC_SYNC
| wxEXEC_NOEVENTS
333 // Execute another program.
335 // If flags contain wxEXEC_SYNC, return -1 on failure and the exit code of the
336 // process if everything was ok. Otherwise (i.e. if wxEXEC_ASYNC), return 0 on
337 // failure and the PID of the launched process if ok.
338 WXDLLIMPEXP_BASE
long wxExecute(wxChar
**argv
, int flags
= wxEXEC_ASYNC
,
339 wxProcess
*process
= (wxProcess
*) NULL
);
340 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
, int flags
= wxEXEC_ASYNC
,
341 wxProcess
*process
= (wxProcess
*) NULL
);
343 // execute the command capturing its output into an array line by line, this is
344 // always synchronous
345 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
346 wxArrayString
& output
,
349 // also capture stderr (also synchronous)
350 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
351 wxArrayString
& output
,
352 wxArrayString
& error
,
355 #if defined(__WXMSW__) && wxUSE_IPC
356 // ask a DDE server to execute the DDE request with given parameters
357 WXDLLIMPEXP_BASE
bool wxExecuteDDE(const wxString
& ddeServer
,
358 const wxString
& ddeTopic
,
359 const wxString
& ddeCommand
);
360 #endif // __WXMSW__ && wxUSE_IPC
364 wxSIGNONE
= 0, // verify if the process exists under Unix
371 wxSIGIOT
= wxSIGABRT
, // another name
382 // further signals are different in meaning between different Unix systems
387 wxKILL_OK
, // no error
388 wxKILL_BAD_SIGNAL
, // no such signal
389 wxKILL_ACCESS_DENIED
, // permission denied
390 wxKILL_NO_PROCESS
, // no such process
391 wxKILL_ERROR
// another, unspecified error
396 wxKILL_NOCHILDREN
= 0, // don't kill children
397 wxKILL_CHILDREN
= 1 // kill children
402 wxSHUTDOWN_POWEROFF
, // power off the computer
403 wxSHUTDOWN_REBOOT
// shutdown and reboot
406 // Shutdown or reboot the PC
407 WXDLLIMPEXP_BASE
bool wxShutdown(wxShutdownFlags wFlags
);
409 // send the given signal to the process (only NONE and KILL are supported under
410 // Windows, all others mean TERM), return 0 if ok and -1 on error
412 // return detailed error in rc if not NULL
413 WXDLLIMPEXP_BASE
int wxKill(long pid
,
414 wxSignal sig
= wxSIGTERM
,
415 wxKillError
*rc
= NULL
,
416 int flags
= wxKILL_NOCHILDREN
);
418 // Execute a command in an interactive shell window (always synchronously)
419 // If no command then just the shell
420 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
= wxEmptyString
);
422 // As wxShell(), but must give a (non interactive) command and its output will
423 // be returned in output array
424 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
, wxArrayString
& output
);
426 // Sleep for nSecs seconds
427 WXDLLIMPEXP_BASE
void wxSleep(int nSecs
);
429 // Sleep for a given amount of milliseconds
430 WXDLLIMPEXP_BASE
void wxMilliSleep(unsigned long milliseconds
);
432 // Sleep for a given amount of microseconds
433 WXDLLIMPEXP_BASE
void wxMicroSleep(unsigned long microseconds
);
435 // Sleep for a given amount of milliseconds (old, bad name), use wxMilliSleep
436 wxDEPRECATED( WXDLLIMPEXP_BASE
void wxUsleep(unsigned long milliseconds
) );
438 // Get the process id of the current process
439 WXDLLIMPEXP_BASE
unsigned long wxGetProcessId();
441 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
442 WXDLLIMPEXP_BASE wxMemorySize
wxGetFreeMemory();
444 #if wxUSE_ON_FATAL_EXCEPTION
446 // should wxApp::OnFatalException() be called?
447 WXDLLIMPEXP_BASE
bool wxHandleFatalExceptions(bool doit
= true);
449 #endif // wxUSE_ON_FATAL_EXCEPTION
451 // flags for wxLaunchDefaultBrowser
454 wxBROWSER_NEW_WINDOW
= 1
457 // Launch url in the user's default internet browser
458 WXDLLIMPEXP_BASE
bool wxLaunchDefaultBrowser(const wxString
& url
, int flags
= 0);
460 // ----------------------------------------------------------------------------
461 // Environment variables
462 // ----------------------------------------------------------------------------
464 // returns true if variable exists (value may be NULL if you just want to check
466 WXDLLIMPEXP_BASE
bool wxGetEnv(const wxString
& var
, wxString
*value
);
468 // set the env var name to the given value, return true on success
469 WXDLLIMPEXP_BASE
bool wxSetEnv(const wxString
& var
, const wxChar
*value
);
471 // remove the env var from environment
472 inline bool wxUnsetEnv(const wxString
& var
) { return wxSetEnv(var
, NULL
); }
474 // ----------------------------------------------------------------------------
475 // Network and username functions.
476 // ----------------------------------------------------------------------------
478 // NB: "char *" functions are deprecated, use wxString ones!
481 WXDLLIMPEXP_BASE
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
482 WXDLLIMPEXP_BASE wxString
wxGetEmailAddress();
485 WXDLLIMPEXP_BASE
bool wxGetHostName(wxChar
*buf
, int maxSize
);
486 WXDLLIMPEXP_BASE wxString
wxGetHostName();
489 WXDLLIMPEXP_BASE wxString
wxGetFullHostName();
490 WXDLLIMPEXP_BASE
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
492 // Get user ID e.g. jacs (this is known as login name under Unix)
493 WXDLLIMPEXP_BASE
bool wxGetUserId(wxChar
*buf
, int maxSize
);
494 WXDLLIMPEXP_BASE wxString
wxGetUserId();
496 // Get user name e.g. Julian Smart
497 WXDLLIMPEXP_BASE
bool wxGetUserName(wxChar
*buf
, int maxSize
);
498 WXDLLIMPEXP_BASE wxString
wxGetUserName();
500 // Get current Home dir and copy to dest (returns pstr->c_str())
501 WXDLLIMPEXP_BASE wxString
wxGetHomeDir();
502 WXDLLIMPEXP_BASE
const wxChar
* wxGetHomeDir(wxString
*pstr
);
504 // Get the user's home dir (caller must copy --- volatile)
505 // returns NULL is no HOME dir is known
506 #if defined(__UNIX__) && wxUSE_UNICODE && !defined(__WINE__)
507 WXDLLIMPEXP_BASE
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
509 WXDLLIMPEXP_BASE wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
513 typedef wxLongLong wxDiskspaceSize_t
;
515 typedef long wxDiskspaceSize_t
;
518 // get number of total/free bytes on the disk where path belongs
519 WXDLLIMPEXP_BASE
bool wxGetDiskSpace(const wxString
& path
,
520 wxDiskspaceSize_t
*pTotal
= NULL
,
521 wxDiskspaceSize_t
*pFree
= NULL
);
523 #if wxUSE_GUI // GUI only things from now on
525 // ----------------------------------------------------------------------------
526 // Menu accelerators related things
527 // ----------------------------------------------------------------------------
529 // flags for wxStripMenuCodes
532 // strip '&' characters
533 wxStrip_Mnemonics
= 1,
535 // strip everything after '\t'
538 // strip everything (this is the default)
539 wxStrip_All
= wxStrip_Mnemonics
| wxStrip_Accel
542 // strip mnemonics and/or accelerators from the label
544 wxStripMenuCodes(const wxString
& str
, int flags
= wxStrip_All
);
546 #if WXWIN_COMPATIBILITY_2_6
547 // obsolete and deprecated version, do not use, use the above overload instead
549 WXDLLEXPORT wxChar
* wxStripMenuCodes(const wxChar
*in
, wxChar
*out
= NULL
)
553 class WXDLLEXPORT wxAcceleratorEntry
;
555 // use wxAcceleratorEntry::Create() or FromString() methods instead
557 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
)
559 #endif // wxUSE_ACCEL
561 #endif // WXWIN_COMPATIBILITY_2_6
563 // ----------------------------------------------------------------------------
565 // ----------------------------------------------------------------------------
567 // Returns menu item id or wxNOT_FOUND if none.
568 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
570 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
571 // is always present but may be less reliable than a native version.
572 WXDLLEXPORT wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
);
573 WXDLLEXPORT wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
);
575 // NB: this function is obsolete, use wxWindow::FindWindowByLabel() instead
577 // Find the window/widget with the given title or label.
578 // Pass a parent to begin the search from, or NULL to look through
580 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
582 // NB: this function is obsolete, use wxWindow::FindWindowByName() instead
584 // Find window by name, and if that fails, by label.
585 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
587 // ----------------------------------------------------------------------------
588 // Message/event queue helpers
589 // ----------------------------------------------------------------------------
591 // Yield to other apps/messages and disable user input
592 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
, bool onlyIfNeeded
= false);
594 // Enable or disable input to all top level windows
595 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= true);
597 // Check whether this window wants to process messages, e.g. Stop button
598 // in long calculations.
599 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
601 // Consume all events until no more left
602 WXDLLEXPORT
void wxFlushEvents();
604 // a class which disables all windows (except, may be, thegiven one) in its
605 // ctor and enables them back in its dtor
606 class WXDLLEXPORT wxWindowDisabler
609 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
613 wxWindowList
*m_winDisabled
;
615 DECLARE_NO_COPY_CLASS(wxWindowDisabler
)
618 // ----------------------------------------------------------------------------
620 // ----------------------------------------------------------------------------
622 // Set the cursor to the busy cursor for all windows
623 WXDLLIMPEXP_CORE
void wxBeginBusyCursor(const wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
625 // Restore cursor to normal
626 WXDLLEXPORT
void wxEndBusyCursor();
628 // true if we're between the above two calls
629 WXDLLEXPORT
bool wxIsBusy();
631 // Convenience class so we can just create a wxBusyCursor object on the stack
632 class WXDLLEXPORT wxBusyCursor
635 wxBusyCursor(const wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
636 { wxBeginBusyCursor(cursor
); }
638 { wxEndBusyCursor(); }
640 // FIXME: These two methods are currently only implemented (and needed?)
641 // in wxGTK. BusyCursor handling should probably be moved to
642 // common code since the wxGTK and wxMSW implementations are very
643 // similar except for wxMSW using HCURSOR directly instead of
645 static const wxCursor
&GetStoredCursor();
646 static const wxCursor
GetBusyCursor();
649 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
651 // MSW only: get user-defined resource from the .res file.
652 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
654 extern WXDLLEXPORT
const wxChar
* wxUserResourceStr
;
655 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
658 // ----------------------------------------------------------------------------
659 // Display and colorss (X only)
660 // ----------------------------------------------------------------------------
663 void *wxGetDisplay();
667 WXDLLIMPEXP_CORE WXDisplay
*wxGetDisplay();
668 WXDLLIMPEXP_CORE
bool wxSetDisplay(const wxString
& display_name
);
669 WXDLLIMPEXP_CORE wxString
wxGetDisplayName();
672 // use this function instead of the functions above in implementation code
673 inline struct _XDisplay
*wxGetX11Display()
675 return (_XDisplay
*)wxGetDisplay();
680 // ----------------------------------------------------------------------------
681 // wxYield(): these functions are obsolete, please use wxApp methods instead!
682 // ----------------------------------------------------------------------------
684 // Yield to other apps/messages
685 WXDLLIMPEXP_BASE
bool wxYield();
687 // Like wxYield, but fails silently if the yield is recursive.
688 WXDLLIMPEXP_BASE
bool wxYieldIfNeeded();
690 // ----------------------------------------------------------------------------
691 // Error message functions used by wxWidgets (deprecated, use wxLog)
692 // ----------------------------------------------------------------------------