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_BASE wxProcess
;
52 class WXDLLIMPEXP_BASE wxFrame
;
53 class WXDLLIMPEXP_BASE wxWindow
;
54 class WXDLLIMPEXP_BASE wxWindowList
;
55 class WXDLLIMPEXP_BASE wxPoint
;
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
62 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
64 // ----------------------------------------------------------------------------
65 // String functions (deprecated, use wxString)
66 // ----------------------------------------------------------------------------
68 // Make a copy of this string using 'new'
69 #if WXWIN_COMPATIBILITY_2_4
70 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 WXDLLIMPEXP_BASE
void wxBell();
83 #if defined(__WXMSW__) || defined(__WXMAC__)
84 // Get the state of a key (true if pressed, false if not)
85 // This is generally most useful getting the state of
86 // Caps Lock, Num Lock and Scroll Lock...
88 // Note - There is a X11/GTK version which will be here
90 WXDLLIMPEXP_BASE
bool wxGetKeyState(wxKeyCode key
);
93 // Get OS description as a user-readable string
94 WXDLLIMPEXP_BASE wxString
wxGetOsDescription();
97 WXDLLIMPEXP_BASE
int wxGetOsVersion(int *majorVsn
= (int *) NULL
,
98 int *minorVsn
= (int *) NULL
);
100 // Return a string with the current date/time
101 WXDLLIMPEXP_BASE wxString
wxNow();
103 // Return path where wxWindows is installed (mostly useful in Unices)
104 WXDLLIMPEXP_BASE
const wxChar
*wxGetInstallPrefix();
105 // Return path to wxWin data (/usr/share/wx/%{version}) (Unices)
106 WXDLLIMPEXP_BASE wxString
wxGetDataDir();
110 // Don't synthesize KeyUp events holding down a key and producing
111 // KeyDown events with autorepeat. On by default and always on
113 WXDLLEXPORT
bool wxSetDetectableAutoRepeat( bool flag
);
115 // ----------------------------------------------------------------------------
116 // Window ID management
117 // ----------------------------------------------------------------------------
119 // Generate a unique ID
120 WXDLLEXPORT
long wxNewId();
122 // Ensure subsequent IDs don't clash with this one
123 WXDLLEXPORT
void wxRegisterId(long id
);
125 // Return the current ID
126 WXDLLEXPORT
long wxGetCurrentId();
130 // ----------------------------------------------------------------------------
131 // Various conversions
132 // ----------------------------------------------------------------------------
134 // these functions are deprecated, use wxString methods instead!
135 #if WXWIN_COMPATIBILITY_2_4
137 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxFloatToStringStr
;
138 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxDoubleToStringStr
;
140 WXDLLIMPEXP_BASE
void StringToFloat(const wxChar
*s
, float *number
);
141 WXDLLIMPEXP_BASE wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
);
142 WXDLLIMPEXP_BASE
void StringToDouble(const wxChar
*s
, double *number
);
143 WXDLLIMPEXP_BASE wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
);
144 WXDLLIMPEXP_BASE
void StringToInt(const wxChar
*s
, int *number
);
145 WXDLLIMPEXP_BASE
void StringToLong(const wxChar
*s
, long *number
);
146 WXDLLIMPEXP_BASE wxChar
* IntToString(int number
);
147 WXDLLIMPEXP_BASE wxChar
* LongToString(long number
);
149 #endif // WXWIN_COMPATIBILITY_2_4
151 // Convert 2-digit hex number to decimal
152 WXDLLIMPEXP_BASE
int wxHexToDec(const wxString
& buf
);
154 // Convert decimal integer to 2-character hex string
155 WXDLLIMPEXP_BASE
void wxDecToHex(int dec
, wxChar
*buf
);
156 WXDLLIMPEXP_BASE wxString
wxDecToHex(int dec
);
158 // ----------------------------------------------------------------------------
159 // Process management
160 // ----------------------------------------------------------------------------
162 // NB: for backwars compatibility reasons the values of wxEXEC_[A]SYNC *must*
163 // be 0 and 1, don't change!
167 // execute the process asynchronously
170 // execute it synchronously, i.e. wait until it finishes
173 // under Windows, don't hide the child even if it's IO is redirected (this
174 // is done by default)
177 // under Unix, if the process is the group leader then killing -pid kills
178 // all children as well as pid
179 wxEXEC_MAKE_GROUP_LEADER
= 4
182 // Execute another program.
184 // If flags contain wxEXEC_SYNC, return -1 on failure and the exit code of the
185 // process if everything was ok. Otherwise (i.e. if wxEXEC_ASYNC), return 0 on
186 // failure and the PID of the launched process if ok.
187 WXDLLIMPEXP_BASE
long wxExecute(wxChar
**argv
, int flags
= wxEXEC_ASYNC
,
188 wxProcess
*process
= (wxProcess
*) NULL
);
189 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
, int flags
= wxEXEC_ASYNC
,
190 wxProcess
*process
= (wxProcess
*) NULL
);
192 // execute the command capturing its output into an array line by line, this is
193 // always synchronous
194 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
195 wxArrayString
& output
);
197 // also capture stderr (also synchronous)
198 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
199 wxArrayString
& output
,
200 wxArrayString
& error
);
204 wxSIGNONE
= 0, // verify if the process exists under Unix
211 wxSIGIOT
= wxSIGABRT
, // another name
222 // further signals are different in meaning between different Unix systems
227 wxKILL_OK
, // no error
228 wxKILL_BAD_SIGNAL
, // no such signal
229 wxKILL_ACCESS_DENIED
, // permission denied
230 wxKILL_NO_PROCESS
, // no such process
231 wxKILL_ERROR
// another, unspecified error
236 wxSHUTDOWN_POWEROFF
, // power off the computer
237 wxSHUTDOWN_REBOOT
// shutdown and reboot
240 // Shutdown or reboot the PC
241 WXDLLIMPEXP_BASE
bool wxShutdown(wxShutdownFlags wFlags
);
243 // send the given signal to the process (only NONE and KILL are supported under
244 // Windows, all others mean TERM), return 0 if ok and -1 on error
246 // return detailed error in rc if not NULL
247 WXDLLIMPEXP_BASE
int wxKill(long pid
,
248 wxSignal sig
= wxSIGTERM
,
249 wxKillError
*rc
= NULL
);
251 // Execute a command in an interactive shell window (always synchronously)
252 // If no command then just the shell
253 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
= wxEmptyString
);
255 // As wxShell(), but must give a (non interactive) command and its output will
256 // be returned in output array
257 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
, wxArrayString
& output
);
259 // Sleep for nSecs seconds
260 WXDLLIMPEXP_BASE
void wxSleep(int nSecs
);
262 // Sleep for a given amount of milliseconds
263 WXDLLIMPEXP_BASE
void wxUsleep(unsigned long milliseconds
);
265 // Get the process id of the current process
266 WXDLLIMPEXP_BASE
unsigned long wxGetProcessId();
268 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
269 WXDLLIMPEXP_BASE
long wxGetFreeMemory();
271 // should wxApp::OnFatalException() be called?
272 WXDLLIMPEXP_BASE
bool wxHandleFatalExceptions(bool doit
= TRUE
);
274 // ----------------------------------------------------------------------------
275 // Environment variables
276 // ----------------------------------------------------------------------------
278 // returns TRUE if variable exists (value may be NULL if you just want to check
280 WXDLLIMPEXP_BASE
bool wxGetEnv(const wxString
& var
, wxString
*value
);
282 // set the env var name to the given value, return TRUE on success
283 WXDLLIMPEXP_BASE
bool wxSetEnv(const wxString
& var
, const wxChar
*value
);
285 // remove the env var from environment
286 inline bool wxUnsetEnv(const wxString
& var
) { return wxSetEnv(var
, NULL
); }
288 // ----------------------------------------------------------------------------
289 // Network and username functions.
290 // ----------------------------------------------------------------------------
292 // NB: "char *" functions are deprecated, use wxString ones!
295 WXDLLIMPEXP_BASE
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
296 WXDLLIMPEXP_BASE wxString
wxGetEmailAddress();
299 WXDLLIMPEXP_BASE
bool wxGetHostName(wxChar
*buf
, int maxSize
);
300 WXDLLIMPEXP_BASE wxString
wxGetHostName();
303 WXDLLIMPEXP_BASE wxString
wxGetFullHostName();
304 WXDLLIMPEXP_BASE
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
306 // Get user ID e.g. jacs (this is known as login name under Unix)
307 WXDLLIMPEXP_BASE
bool wxGetUserId(wxChar
*buf
, int maxSize
);
308 WXDLLIMPEXP_BASE wxString
wxGetUserId();
310 // Get user name e.g. Julian Smart
311 WXDLLIMPEXP_BASE
bool wxGetUserName(wxChar
*buf
, int maxSize
);
312 WXDLLIMPEXP_BASE wxString
wxGetUserName();
314 // Get current Home dir and copy to dest (returns pstr->c_str())
315 WXDLLIMPEXP_BASE wxString
wxGetHomeDir();
316 WXDLLIMPEXP_BASE
const wxChar
* wxGetHomeDir(wxString
*pstr
);
318 // Get the user's home dir (caller must copy --- volatile)
319 // returns NULL is no HOME dir is known
320 #if defined(__UNIX__) && wxUSE_UNICODE
321 WXDLLIMPEXP_BASE
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
323 WXDLLIMPEXP_BASE wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
326 // get number of total/free bytes on the disk where path belongs
327 WXDLLIMPEXP_BASE
bool wxGetDiskSpace(const wxString
& path
,
328 wxLongLong
*pTotal
= NULL
,
329 wxLongLong
*pFree
= NULL
);
331 #if wxUSE_GUI // GUI only things from now on
333 // ----------------------------------------------------------------------------
334 // Menu accelerators related things
335 // ----------------------------------------------------------------------------
337 WXDLLEXPORT wxChar
* wxStripMenuCodes(const wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
338 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
341 class WXDLLEXPORT wxAcceleratorEntry
;
342 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
343 #endif // wxUSE_ACCEL
345 // ----------------------------------------------------------------------------
347 // ----------------------------------------------------------------------------
349 // Returns menu item id or -1 if none.
350 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
352 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
353 // is always present but may be less reliable than a native version.
354 WXDLLEXPORT wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
);
355 WXDLLEXPORT wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
);
357 // NB: this function is obsolete, use wxWindow::FindWindowByLabel() instead
359 // Find the window/widget with the given title or label.
360 // Pass a parent to begin the search from, or NULL to look through
362 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
364 // NB: this function is obsolete, use wxWindow::FindWindowByName() instead
366 // Find window by name, and if that fails, by label.
367 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
369 // ----------------------------------------------------------------------------
370 // Message/event queue helpers
371 // ----------------------------------------------------------------------------
373 // Yield to other apps/messages and disable user input
374 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
, bool onlyIfNeeded
= FALSE
);
376 // Enable or disable input to all top level windows
377 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= TRUE
);
379 // Check whether this window wants to process messages, e.g. Stop button
380 // in long calculations.
381 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
383 // Consume all events until no more left
384 WXDLLEXPORT
void wxFlushEvents();
386 // a class which disables all windows (except, may be, thegiven one) in its
387 // ctor and enables them back in its dtor
388 class WXDLLEXPORT wxWindowDisabler
391 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
395 wxWindowList
*m_winDisabled
;
397 DECLARE_NO_COPY_CLASS(wxWindowDisabler
)
400 // ----------------------------------------------------------------------------
402 // ----------------------------------------------------------------------------
404 // Set the cursor to the busy cursor for all windows
405 class WXDLLEXPORT wxCursor
;
406 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
407 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
409 // Restore cursor to normal
410 WXDLLEXPORT
void wxEndBusyCursor();
412 // TRUE if we're between the above two calls
413 WXDLLEXPORT
bool wxIsBusy();
415 // Convenience class so we can just create a wxBusyCursor object on the stack
416 class WXDLLEXPORT wxBusyCursor
419 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
420 { wxBeginBusyCursor(cursor
); }
422 { wxEndBusyCursor(); }
424 // FIXME: These two methods are currently only implemented (and needed?)
425 // in wxGTK. BusyCursor handling should probably be moved to
426 // common code since the wxGTK and wxMSW implementations are very
427 // similar except for wxMSW using HCURSOR directly instead of
429 static const wxCursor
&GetStoredCursor();
430 static const wxCursor
GetBusyCursor();
434 // ----------------------------------------------------------------------------
435 // Reading and writing resources (eg WIN.INI, .Xdefaults)
436 // ----------------------------------------------------------------------------
439 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
440 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
441 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
442 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
444 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
445 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
446 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
447 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
448 #endif // wxUSE_RESOURCES
450 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
452 // MSW only: get user-defined resource from the .res file.
453 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
455 WXDLLEXPORT
extern const wxChar
* wxUserResourceStr
;
456 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
459 // ----------------------------------------------------------------------------
460 // Display and colorss (X only)
461 // ----------------------------------------------------------------------------
464 void *wxGetDisplay();
468 WXDisplay
*wxGetDisplay();
469 bool wxSetDisplay(const wxString
& display_name
);
470 wxString
wxGetDisplayName();
475 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
476 // The resulting warnings are switched off here
477 #pragma message disable nosimpint
479 // #include <X11/Xlib.h>
481 #pragma message enable nosimpint
488 // ----------------------------------------------------------------------------
489 // wxYield(): these functions are obsolete, please use wxApp methods instead!
490 // ----------------------------------------------------------------------------
492 // Yield to other apps/messages
493 WXDLLIMPEXP_BASE
bool wxYield();
495 // Like wxYield, but fails silently if the yield is recursive.
496 WXDLLIMPEXP_BASE
bool wxYieldIfNeeded();
498 // ----------------------------------------------------------------------------
499 // Error message functions used by wxWindows (deprecated, use wxLog)
500 // ----------------------------------------------------------------------------
502 #if WXWIN_COMPATIBILITY_2_2
504 // Format a message on the standard error (UNIX) or the debugging
506 WXDLLIMPEXP_BASE
void wxDebugMsg(const wxChar
*fmt
...) ATTRIBUTE_PRINTF_1
;
508 // Non-fatal error (continues)
509 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxInternalErrorStr
;
510 WXDLLIMPEXP_BASE
void wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
512 // Fatal error (exits)
513 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxFatalErrorStr
;
514 WXDLLIMPEXP_BASE
void wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);
516 #endif // WXWIN_COMPATIBILITY_2_2