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(__APPLE__)
20 #pragma interface "utils.h"
23 #include "wx/object.h"
25 #include "wx/filefn.h"
27 // need this for wxGetDiskSpace() as we can't, unfortunately, forward declare
29 #include "wx/longlong.h"
38 // ----------------------------------------------------------------------------
39 // Forward declaration
40 // ----------------------------------------------------------------------------
42 class WXDLLIMPEXP_BASE wxProcess
;
43 class WXDLLIMPEXP_BASE wxFrame
;
44 class WXDLLIMPEXP_BASE wxWindow
;
45 class WXDLLIMPEXP_BASE wxWindowList
;
46 class WXDLLIMPEXP_BASE wxPoint
;
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
53 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
55 // ----------------------------------------------------------------------------
56 // String functions (deprecated, use wxString)
57 // ----------------------------------------------------------------------------
59 // Make a copy of this string using 'new'
60 #if WXWIN_COMPATIBILITY_2_4
61 WXDLLIMPEXP_BASE wxChar
* copystring(const wxChar
*s
);
64 #if WXWIN_COMPATIBILITY_2
65 // Matches string one within string two regardless of case
66 WXDLLIMPEXP_BASE
bool StringMatch(const wxChar
*one
, const wxChar
*two
, bool subString
= TRUE
, bool exact
= FALSE
);
69 // A shorter way of using strcmp
70 #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
72 // ----------------------------------------------------------------------------
73 // Miscellaneous functions
74 // ----------------------------------------------------------------------------
77 WXDLLIMPEXP_BASE
void wxBell();
79 // Get OS description as a user-readable string
80 WXDLLIMPEXP_BASE wxString
wxGetOsDescription();
83 WXDLLIMPEXP_BASE
int wxGetOsVersion(int *majorVsn
= (int *) NULL
,
84 int *minorVsn
= (int *) NULL
);
86 // Return a string with the current date/time
87 WXDLLIMPEXP_BASE wxString
wxNow();
89 // Return path where wxWindows is installed (mostly useful in Unices)
90 WXDLLIMPEXP_BASE
const wxChar
*wxGetInstallPrefix();
91 // Return path to wxWin data (/usr/share/wx/%{version}) (Unices)
92 WXDLLIMPEXP_BASE wxString
wxGetDataDir();
96 // Don't synthesize KeyUp events holding down a key and producing
97 // KeyDown events with autorepeat. On by default and always on
99 WXDLLEXPORT
bool wxSetDetectableAutoRepeat( bool flag
);
101 // ----------------------------------------------------------------------------
102 // Window ID management
103 // ----------------------------------------------------------------------------
105 // Generate a unique ID
106 WXDLLEXPORT
long wxNewId();
107 #if !defined(NewId) && defined(WXWIN_COMPATIBILITY)
108 #define NewId wxNewId
111 // Ensure subsequent IDs don't clash with this one
112 WXDLLEXPORT
void wxRegisterId(long id
);
113 #if !defined(RegisterId) && defined(WXWIN_COMPATIBILITY)
114 #define RegisterId wxRegisterId
117 // Return the current ID
118 WXDLLEXPORT
long wxGetCurrentId();
122 // ----------------------------------------------------------------------------
123 // Various conversions
124 // ----------------------------------------------------------------------------
126 // these functions are deprecated, use wxString methods instead!
127 #if WXWIN_COMPATIBILITY_2_4
129 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxFloatToStringStr
;
130 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxDoubleToStringStr
;
132 WXDLLIMPEXP_BASE
void StringToFloat(const wxChar
*s
, float *number
);
133 WXDLLIMPEXP_BASE wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
);
134 WXDLLIMPEXP_BASE
void StringToDouble(const wxChar
*s
, double *number
);
135 WXDLLIMPEXP_BASE wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
);
136 WXDLLIMPEXP_BASE
void StringToInt(const wxChar
*s
, int *number
);
137 WXDLLIMPEXP_BASE
void StringToLong(const wxChar
*s
, long *number
);
138 WXDLLIMPEXP_BASE wxChar
* IntToString(int number
);
139 WXDLLIMPEXP_BASE wxChar
* LongToString(long number
);
141 #endif // WXWIN_COMPATIBILITY_2_4
143 // Convert 2-digit hex number to decimal
144 WXDLLIMPEXP_BASE
int wxHexToDec(const wxString
& buf
);
146 // Convert decimal integer to 2-character hex string
147 WXDLLIMPEXP_BASE
void wxDecToHex(int dec
, wxChar
*buf
);
148 WXDLLIMPEXP_BASE wxString
wxDecToHex(int dec
);
150 // ----------------------------------------------------------------------------
151 // Process management
152 // ----------------------------------------------------------------------------
154 // NB: for backwars compatibility reasons the values of wxEXEC_[A]SYNC *must*
155 // be 0 and 1, don't change!
159 // execute the process asynchronously
162 // execute it synchronously, i.e. wait until it finishes
165 // under Windows, don't hide the child even if it's IO is redirected (this
166 // is done by default)
169 // under Unix, if the process is the group leader then killing -pid kills
170 // all children as well as pid
171 wxEXEC_MAKE_GROUP_LEADER
= 4
174 // Execute another program.
176 // If flags contain wxEXEC_SYNC, return -1 on failure and the exit code of the
177 // process if everything was ok. Otherwise (i.e. if wxEXEC_ASYNC), return 0 on
178 // failure and the PID of the launched process if ok.
179 WXDLLIMPEXP_BASE
long wxExecute(wxChar
**argv
, int flags
= wxEXEC_ASYNC
,
180 wxProcess
*process
= (wxProcess
*) NULL
);
181 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
, int flags
= wxEXEC_ASYNC
,
182 wxProcess
*process
= (wxProcess
*) NULL
);
184 // execute the command capturing its output into an array line by line, this is
185 // always synchronous
186 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
187 wxArrayString
& output
);
189 // also capture stderr (also synchronous)
190 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
191 wxArrayString
& output
,
192 wxArrayString
& error
);
196 wxSIGNONE
= 0, // verify if the process exists under Unix
203 wxSIGIOT
= wxSIGABRT
, // another name
214 // further signals are different in meaning between different Unix systems
219 wxKILL_OK
, // no error
220 wxKILL_BAD_SIGNAL
, // no such signal
221 wxKILL_ACCESS_DENIED
, // permission denied
222 wxKILL_NO_PROCESS
, // no such process
223 wxKILL_ERROR
// another, unspecified error
228 wxSHUTDOWN_POWEROFF
, // power off the computer
229 wxSHUTDOWN_REBOOT
// shutdown and reboot
232 // Shutdown or reboot the PC
233 WXDLLIMPEXP_BASE
bool wxShutdown(wxShutdownFlags wFlags
);
235 // send the given signal to the process (only NONE and KILL are supported under
236 // Windows, all others mean TERM), return 0 if ok and -1 on error
238 // return detailed error in rc if not NULL
239 WXDLLIMPEXP_BASE
int wxKill(long pid
,
240 wxSignal sig
= wxSIGTERM
,
241 wxKillError
*rc
= NULL
);
243 // Execute a command in an interactive shell window (always synchronously)
244 // If no command then just the shell
245 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
= wxEmptyString
);
247 // As wxShell(), but must give a (non interactive) command and its output will
248 // be returned in output array
249 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
, wxArrayString
& output
);
251 // Sleep for nSecs seconds
252 WXDLLIMPEXP_BASE
void wxSleep(int nSecs
);
254 // Sleep for a given amount of milliseconds
255 WXDLLIMPEXP_BASE
void wxUsleep(unsigned long milliseconds
);
257 // Get the process id of the current process
258 WXDLLIMPEXP_BASE
unsigned long wxGetProcessId();
260 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
261 WXDLLIMPEXP_BASE
long wxGetFreeMemory();
263 // should wxApp::OnFatalException() be called?
264 WXDLLIMPEXP_BASE
bool wxHandleFatalExceptions(bool doit
= TRUE
);
266 // ----------------------------------------------------------------------------
267 // Environment variables
268 // ----------------------------------------------------------------------------
270 // returns TRUE if variable exists (value may be NULL if you just want to check
272 WXDLLIMPEXP_BASE
bool wxGetEnv(const wxString
& var
, wxString
*value
);
274 // set the env var name to the given value, return TRUE on success
275 WXDLLIMPEXP_BASE
bool wxSetEnv(const wxString
& var
, const wxChar
*value
);
277 // remove the env var from environment
278 inline bool wxUnsetEnv(const wxString
& var
) { return wxSetEnv(var
, NULL
); }
280 // ----------------------------------------------------------------------------
281 // Network and username functions.
282 // ----------------------------------------------------------------------------
284 // NB: "char *" functions are deprecated, use wxString ones!
287 WXDLLIMPEXP_BASE
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
288 WXDLLIMPEXP_BASE wxString
wxGetEmailAddress();
291 WXDLLIMPEXP_BASE
bool wxGetHostName(wxChar
*buf
, int maxSize
);
292 WXDLLIMPEXP_BASE wxString
wxGetHostName();
295 WXDLLIMPEXP_BASE wxString
wxGetFullHostName();
296 WXDLLIMPEXP_BASE
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
298 // Get user ID e.g. jacs (this is known as login name under Unix)
299 WXDLLIMPEXP_BASE
bool wxGetUserId(wxChar
*buf
, int maxSize
);
300 WXDLLIMPEXP_BASE wxString
wxGetUserId();
302 // Get user name e.g. Julian Smart
303 WXDLLIMPEXP_BASE
bool wxGetUserName(wxChar
*buf
, int maxSize
);
304 WXDLLIMPEXP_BASE wxString
wxGetUserName();
306 // Get current Home dir and copy to dest (returns pstr->c_str())
307 WXDLLIMPEXP_BASE wxString
wxGetHomeDir();
308 WXDLLIMPEXP_BASE
const wxChar
* wxGetHomeDir(wxString
*pstr
);
310 // Get the user's home dir (caller must copy --- volatile)
311 // returns NULL is no HOME dir is known
312 #if defined(__UNIX__) && wxUSE_UNICODE
313 WXDLLIMPEXP_BASE
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
315 WXDLLIMPEXP_BASE wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
318 // get number of total/free bytes on the disk where path belongs
319 WXDLLIMPEXP_BASE
bool wxGetDiskSpace(const wxString
& path
,
320 wxLongLong
*pTotal
= NULL
,
321 wxLongLong
*pFree
= NULL
);
323 #if wxUSE_GUI // GUI only things from now on
325 // ----------------------------------------------------------------------------
326 // Menu accelerators related things
327 // ----------------------------------------------------------------------------
329 WXDLLEXPORT wxChar
* wxStripMenuCodes(const wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
330 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
333 class WXDLLEXPORT wxAcceleratorEntry
;
334 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
335 #endif // wxUSE_ACCEL
337 // ----------------------------------------------------------------------------
339 // ----------------------------------------------------------------------------
341 // Returns menu item id or -1 if none.
342 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
344 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
345 // is always present but may be less reliable than a native version.
346 WXDLLEXPORT wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
);
347 WXDLLEXPORT wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
);
349 // NB: this function is obsolete, use wxWindow::FindWindowByLabel() instead
351 // Find the window/widget with the given title or label.
352 // Pass a parent to begin the search from, or NULL to look through
354 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
356 // NB: this function is obsolete, use wxWindow::FindWindowByName() instead
358 // Find window by name, and if that fails, by label.
359 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
361 // ----------------------------------------------------------------------------
362 // Message/event queue helpers
363 // ----------------------------------------------------------------------------
365 // Yield to other apps/messages and disable user input
366 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
, bool onlyIfNeeded
= FALSE
);
368 // Enable or disable input to all top level windows
369 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= TRUE
);
371 // Check whether this window wants to process messages, e.g. Stop button
372 // in long calculations.
373 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
375 // Consume all events until no more left
376 WXDLLEXPORT
void wxFlushEvents();
378 // a class which disables all windows (except, may be, thegiven one) in its
379 // ctor and enables them back in its dtor
380 class WXDLLEXPORT wxWindowDisabler
383 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
387 wxWindowList
*m_winDisabled
;
389 DECLARE_NO_COPY_CLASS(wxWindowDisabler
)
392 // ----------------------------------------------------------------------------
394 // ----------------------------------------------------------------------------
396 // Set the cursor to the busy cursor for all windows
397 class WXDLLEXPORT wxCursor
;
398 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
399 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
401 // Restore cursor to normal
402 WXDLLEXPORT
void wxEndBusyCursor();
404 // TRUE if we're between the above two calls
405 WXDLLEXPORT
bool wxIsBusy();
407 // Convenience class so we can just create a wxBusyCursor object on the stack
408 class WXDLLEXPORT wxBusyCursor
411 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
412 { wxBeginBusyCursor(cursor
); }
414 { wxEndBusyCursor(); }
416 // FIXME: These two methods are currently only implemented (and needed?)
417 // in wxGTK. BusyCursor handling should probably be moved to
418 // common code since the wxGTK and wxMSW implementations are very
419 // similar except for wxMSW using HCURSOR directly instead of
421 static const wxCursor
&GetStoredCursor();
422 static const wxCursor
GetBusyCursor();
426 // ----------------------------------------------------------------------------
427 // Reading and writing resources (eg WIN.INI, .Xdefaults)
428 // ----------------------------------------------------------------------------
431 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
432 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
433 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
434 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
436 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
437 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
438 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
439 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
440 #endif // wxUSE_RESOURCES
442 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
444 // MSW only: get user-defined resource from the .res file.
445 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
447 WXDLLEXPORT
extern const wxChar
* wxUserResourceStr
;
448 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
451 // ----------------------------------------------------------------------------
452 // Display and colorss (X only)
453 // ----------------------------------------------------------------------------
456 void *wxGetDisplay();
460 WXDisplay
*wxGetDisplay();
461 bool wxSetDisplay(const wxString
& display_name
);
462 wxString
wxGetDisplayName();
467 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
468 // The resulting warnings are switched off here
469 #pragma message disable nosimpint
471 // #include <X11/Xlib.h>
473 #pragma message enable nosimpint
480 // ----------------------------------------------------------------------------
481 // wxYield(): these functions are obsolete, please use wxApp methods instead!
482 // ----------------------------------------------------------------------------
484 // Yield to other apps/messages
485 WXDLLIMPEXP_BASE
bool wxYield();
487 // Like wxYield, but fails silently if the yield is recursive.
488 WXDLLIMPEXP_BASE
bool wxYieldIfNeeded();
490 // ----------------------------------------------------------------------------
491 // Error message functions used by wxWindows (deprecated, use wxLog)
492 // ----------------------------------------------------------------------------
494 #if WXWIN_COMPATIBILITY_2_2
496 // Format a message on the standard error (UNIX) or the debugging
498 WXDLLIMPEXP_BASE
void wxDebugMsg(const wxChar
*fmt
...) ATTRIBUTE_PRINTF_1
;
500 // Non-fatal error (continues)
501 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxInternalErrorStr
;
502 WXDLLIMPEXP_BASE
void wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
504 // Fatal error (exits)
505 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxFatalErrorStr
;
506 WXDLLIMPEXP_BASE
void wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);
508 #endif // WXWIN_COMPATIBILITY_2_2