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"
26 #include "wx/arrstr.h"
28 // need this for wxGetDiskSpace() as we can't, unfortunately, forward declare
30 #include "wx/longlong.h"
39 // ----------------------------------------------------------------------------
40 // Forward declaration
41 // ----------------------------------------------------------------------------
43 class WXDLLIMPEXP_BASE wxProcess
;
44 class WXDLLIMPEXP_BASE wxFrame
;
45 class WXDLLIMPEXP_BASE wxWindow
;
46 class WXDLLIMPEXP_BASE wxWindowList
;
47 class WXDLLIMPEXP_BASE wxPoint
;
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
54 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
56 // ----------------------------------------------------------------------------
57 // String functions (deprecated, use wxString)
58 // ----------------------------------------------------------------------------
60 // Make a copy of this string using 'new'
61 #if WXWIN_COMPATIBILITY_2_4
62 WXDLLIMPEXP_BASE wxChar
* copystring(const wxChar
*s
);
65 #if WXWIN_COMPATIBILITY_2
66 // Matches string one within string two regardless of case
67 WXDLLIMPEXP_BASE
bool StringMatch(const wxChar
*one
, const wxChar
*two
, bool subString
= TRUE
, bool exact
= FALSE
);
70 // A shorter way of using strcmp
71 #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
73 // ----------------------------------------------------------------------------
74 // Miscellaneous functions
75 // ----------------------------------------------------------------------------
78 WXDLLIMPEXP_BASE
void wxBell();
80 // Get OS description as a user-readable string
81 WXDLLIMPEXP_BASE wxString
wxGetOsDescription();
84 WXDLLIMPEXP_BASE
int wxGetOsVersion(int *majorVsn
= (int *) NULL
,
85 int *minorVsn
= (int *) NULL
);
87 // Return a string with the current date/time
88 WXDLLIMPEXP_BASE wxString
wxNow();
90 // Return path where wxWindows is installed (mostly useful in Unices)
91 WXDLLIMPEXP_BASE
const wxChar
*wxGetInstallPrefix();
92 // Return path to wxWin data (/usr/share/wx/%{version}) (Unices)
93 WXDLLIMPEXP_BASE wxString
wxGetDataDir();
97 // Don't synthesize KeyUp events holding down a key and producing
98 // KeyDown events with autorepeat. On by default and always on
100 WXDLLEXPORT
bool wxSetDetectableAutoRepeat( bool flag
);
102 // ----------------------------------------------------------------------------
103 // Window ID management
104 // ----------------------------------------------------------------------------
106 // Generate a unique ID
107 WXDLLEXPORT
long wxNewId();
108 #if !defined(NewId) && defined(WXWIN_COMPATIBILITY)
109 #define NewId wxNewId
112 // Ensure subsequent IDs don't clash with this one
113 WXDLLEXPORT
void wxRegisterId(long id
);
114 #if !defined(RegisterId) && defined(WXWIN_COMPATIBILITY)
115 #define RegisterId wxRegisterId
118 // Return the current ID
119 WXDLLEXPORT
long wxGetCurrentId();
123 // ----------------------------------------------------------------------------
124 // Various conversions
125 // ----------------------------------------------------------------------------
127 // these functions are deprecated, use wxString methods instead!
128 #if WXWIN_COMPATIBILITY_2_4
130 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxFloatToStringStr
;
131 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxDoubleToStringStr
;
133 WXDLLIMPEXP_BASE
void StringToFloat(const wxChar
*s
, float *number
);
134 WXDLLIMPEXP_BASE wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
);
135 WXDLLIMPEXP_BASE
void StringToDouble(const wxChar
*s
, double *number
);
136 WXDLLIMPEXP_BASE wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
);
137 WXDLLIMPEXP_BASE
void StringToInt(const wxChar
*s
, int *number
);
138 WXDLLIMPEXP_BASE
void StringToLong(const wxChar
*s
, long *number
);
139 WXDLLIMPEXP_BASE wxChar
* IntToString(int number
);
140 WXDLLIMPEXP_BASE wxChar
* LongToString(long number
);
142 #endif // WXWIN_COMPATIBILITY_2_4
144 // Convert 2-digit hex number to decimal
145 WXDLLIMPEXP_BASE
int wxHexToDec(const wxString
& buf
);
147 // Convert decimal integer to 2-character hex string
148 WXDLLIMPEXP_BASE
void wxDecToHex(int dec
, wxChar
*buf
);
149 WXDLLIMPEXP_BASE wxString
wxDecToHex(int dec
);
151 // ----------------------------------------------------------------------------
152 // Process management
153 // ----------------------------------------------------------------------------
155 // NB: for backwars compatibility reasons the values of wxEXEC_[A]SYNC *must*
156 // be 0 and 1, don't change!
160 // execute the process asynchronously
163 // execute it synchronously, i.e. wait until it finishes
166 // under Windows, don't hide the child even if it's IO is redirected (this
167 // is done by default)
170 // under Unix, if the process is the group leader then killing -pid kills
171 // all children as well as pid
172 wxEXEC_MAKE_GROUP_LEADER
= 4
175 // Execute another program.
177 // If flags contain wxEXEC_SYNC, return -1 on failure and the exit code of the
178 // process if everything was ok. Otherwise (i.e. if wxEXEC_ASYNC), return 0 on
179 // failure and the PID of the launched process if ok.
180 WXDLLIMPEXP_BASE
long wxExecute(wxChar
**argv
, int flags
= wxEXEC_ASYNC
,
181 wxProcess
*process
= (wxProcess
*) NULL
);
182 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
, int flags
= wxEXEC_ASYNC
,
183 wxProcess
*process
= (wxProcess
*) NULL
);
185 // execute the command capturing its output into an array line by line, this is
186 // always synchronous
187 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
188 wxArrayString
& output
);
190 // also capture stderr (also synchronous)
191 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
192 wxArrayString
& output
,
193 wxArrayString
& error
);
197 wxSIGNONE
= 0, // verify if the process exists under Unix
204 wxSIGIOT
= wxSIGABRT
, // another name
215 // further signals are different in meaning between different Unix systems
220 wxKILL_OK
, // no error
221 wxKILL_BAD_SIGNAL
, // no such signal
222 wxKILL_ACCESS_DENIED
, // permission denied
223 wxKILL_NO_PROCESS
, // no such process
224 wxKILL_ERROR
// another, unspecified error
229 wxSHUTDOWN_POWEROFF
, // power off the computer
230 wxSHUTDOWN_REBOOT
// shutdown and reboot
233 // Shutdown or reboot the PC
234 WXDLLIMPEXP_BASE
bool wxShutdown(wxShutdownFlags wFlags
);
236 // send the given signal to the process (only NONE and KILL are supported under
237 // Windows, all others mean TERM), return 0 if ok and -1 on error
239 // return detailed error in rc if not NULL
240 WXDLLIMPEXP_BASE
int wxKill(long pid
,
241 wxSignal sig
= wxSIGTERM
,
242 wxKillError
*rc
= NULL
);
244 // Execute a command in an interactive shell window (always synchronously)
245 // If no command then just the shell
246 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
= wxEmptyString
);
248 // As wxShell(), but must give a (non interactive) command and its output will
249 // be returned in output array
250 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
, wxArrayString
& output
);
252 // Sleep for nSecs seconds
253 WXDLLIMPEXP_BASE
void wxSleep(int nSecs
);
255 // Sleep for a given amount of milliseconds
256 WXDLLIMPEXP_BASE
void wxUsleep(unsigned long milliseconds
);
258 // Get the process id of the current process
259 WXDLLIMPEXP_BASE
unsigned long wxGetProcessId();
261 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
262 WXDLLIMPEXP_BASE
long wxGetFreeMemory();
264 // should wxApp::OnFatalException() be called?
265 WXDLLIMPEXP_BASE
bool wxHandleFatalExceptions(bool doit
= TRUE
);
267 // ----------------------------------------------------------------------------
268 // Environment variables
269 // ----------------------------------------------------------------------------
271 // returns TRUE if variable exists (value may be NULL if you just want to check
273 WXDLLIMPEXP_BASE
bool wxGetEnv(const wxString
& var
, wxString
*value
);
275 // set the env var name to the given value, return TRUE on success
276 WXDLLIMPEXP_BASE
bool wxSetEnv(const wxString
& var
, const wxChar
*value
);
278 // remove the env var from environment
279 inline bool wxUnsetEnv(const wxString
& var
) { return wxSetEnv(var
, NULL
); }
281 // ----------------------------------------------------------------------------
282 // Network and username functions.
283 // ----------------------------------------------------------------------------
285 // NB: "char *" functions are deprecated, use wxString ones!
288 WXDLLIMPEXP_BASE
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
289 WXDLLIMPEXP_BASE wxString
wxGetEmailAddress();
292 WXDLLIMPEXP_BASE
bool wxGetHostName(wxChar
*buf
, int maxSize
);
293 WXDLLIMPEXP_BASE wxString
wxGetHostName();
296 WXDLLIMPEXP_BASE wxString
wxGetFullHostName();
297 WXDLLIMPEXP_BASE
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
299 // Get user ID e.g. jacs (this is known as login name under Unix)
300 WXDLLIMPEXP_BASE
bool wxGetUserId(wxChar
*buf
, int maxSize
);
301 WXDLLIMPEXP_BASE wxString
wxGetUserId();
303 // Get user name e.g. Julian Smart
304 WXDLLIMPEXP_BASE
bool wxGetUserName(wxChar
*buf
, int maxSize
);
305 WXDLLIMPEXP_BASE wxString
wxGetUserName();
307 // Get current Home dir and copy to dest (returns pstr->c_str())
308 WXDLLIMPEXP_BASE wxString
wxGetHomeDir();
309 WXDLLIMPEXP_BASE
const wxChar
* wxGetHomeDir(wxString
*pstr
);
311 // Get the user's home dir (caller must copy --- volatile)
312 // returns NULL is no HOME dir is known
313 #if defined(__UNIX__) && wxUSE_UNICODE
314 WXDLLIMPEXP_BASE
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
316 WXDLLIMPEXP_BASE wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
319 // get number of total/free bytes on the disk where path belongs
320 WXDLLIMPEXP_BASE
bool wxGetDiskSpace(const wxString
& path
,
321 wxLongLong
*pTotal
= NULL
,
322 wxLongLong
*pFree
= NULL
);
324 #if wxUSE_GUI // GUI only things from now on
326 // ----------------------------------------------------------------------------
327 // Menu accelerators related things
328 // ----------------------------------------------------------------------------
330 WXDLLEXPORT wxChar
* wxStripMenuCodes(const wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
331 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
334 class WXDLLEXPORT wxAcceleratorEntry
;
335 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
336 #endif // wxUSE_ACCEL
338 // ----------------------------------------------------------------------------
340 // ----------------------------------------------------------------------------
342 // Returns menu item id or -1 if none.
343 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
345 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
346 // is always present but may be less reliable than a native version.
347 WXDLLEXPORT wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
);
348 WXDLLEXPORT wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
);
350 // NB: this function is obsolete, use wxWindow::FindWindowByLabel() instead
352 // Find the window/widget with the given title or label.
353 // Pass a parent to begin the search from, or NULL to look through
355 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
357 // NB: this function is obsolete, use wxWindow::FindWindowByName() instead
359 // Find window by name, and if that fails, by label.
360 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
362 // ----------------------------------------------------------------------------
363 // Message/event queue helpers
364 // ----------------------------------------------------------------------------
366 // Yield to other apps/messages and disable user input
367 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
, bool onlyIfNeeded
= FALSE
);
369 // Enable or disable input to all top level windows
370 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= TRUE
);
372 // Check whether this window wants to process messages, e.g. Stop button
373 // in long calculations.
374 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
376 // Consume all events until no more left
377 WXDLLEXPORT
void wxFlushEvents();
379 // a class which disables all windows (except, may be, thegiven one) in its
380 // ctor and enables them back in its dtor
381 class WXDLLEXPORT wxWindowDisabler
384 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
388 wxWindowList
*m_winDisabled
;
390 DECLARE_NO_COPY_CLASS(wxWindowDisabler
)
393 // ----------------------------------------------------------------------------
395 // ----------------------------------------------------------------------------
397 // Set the cursor to the busy cursor for all windows
398 class WXDLLEXPORT wxCursor
;
399 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
400 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
402 // Restore cursor to normal
403 WXDLLEXPORT
void wxEndBusyCursor();
405 // TRUE if we're between the above two calls
406 WXDLLEXPORT
bool wxIsBusy();
408 // Convenience class so we can just create a wxBusyCursor object on the stack
409 class WXDLLEXPORT wxBusyCursor
412 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
413 { wxBeginBusyCursor(cursor
); }
415 { wxEndBusyCursor(); }
417 // FIXME: These two methods are currently only implemented (and needed?)
418 // in wxGTK. BusyCursor handling should probably be moved to
419 // common code since the wxGTK and wxMSW implementations are very
420 // similar except for wxMSW using HCURSOR directly instead of
422 static const wxCursor
&GetStoredCursor();
423 static const wxCursor
GetBusyCursor();
427 // ----------------------------------------------------------------------------
428 // Reading and writing resources (eg WIN.INI, .Xdefaults)
429 // ----------------------------------------------------------------------------
432 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
433 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
434 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
435 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
437 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
438 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
439 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
440 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
441 #endif // wxUSE_RESOURCES
443 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
445 // MSW only: get user-defined resource from the .res file.
446 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
448 WXDLLEXPORT
extern const wxChar
* wxUserResourceStr
;
449 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
452 // ----------------------------------------------------------------------------
453 // Display and colorss (X only)
454 // ----------------------------------------------------------------------------
457 void *wxGetDisplay();
461 WXDisplay
*wxGetDisplay();
462 bool wxSetDisplay(const wxString
& display_name
);
463 wxString
wxGetDisplayName();
468 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
469 // The resulting warnings are switched off here
470 #pragma message disable nosimpint
472 // #include <X11/Xlib.h>
474 #pragma message enable nosimpint
481 // ----------------------------------------------------------------------------
482 // wxYield(): these functions are obsolete, please use wxApp methods instead!
483 // ----------------------------------------------------------------------------
485 // Yield to other apps/messages
486 WXDLLIMPEXP_BASE
bool wxYield();
488 // Like wxYield, but fails silently if the yield is recursive.
489 WXDLLIMPEXP_BASE
bool wxYieldIfNeeded();
491 // ----------------------------------------------------------------------------
492 // Error message functions used by wxWindows (deprecated, use wxLog)
493 // ----------------------------------------------------------------------------
495 #if WXWIN_COMPATIBILITY_2_2
497 // Format a message on the standard error (UNIX) or the debugging
499 WXDLLIMPEXP_BASE
void wxDebugMsg(const wxChar
*fmt
...) ATTRIBUTE_PRINTF_1
;
501 // Non-fatal error (continues)
502 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxInternalErrorStr
;
503 WXDLLIMPEXP_BASE
void wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
505 // Fatal error (exits)
506 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxFatalErrorStr
;
507 WXDLLIMPEXP_BASE
void wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);
509 #endif // WXWIN_COMPATIBILITY_2_2