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)
20 #pragma interface "utils.h"
23 #include "wx/object.h"
25 #include "wx/filefn.h"
27 class WXDLLIMPEXP_BASE wxArrayString
;
29 // need this for wxGetDiskSpace() as we can't, unfortunately, forward declare
31 #include "wx/longlong.h"
40 // ----------------------------------------------------------------------------
41 // Forward declaration
42 // ----------------------------------------------------------------------------
44 class WXDLLIMPEXP_BASE wxProcess
;
45 class WXDLLIMPEXP_BASE wxFrame
;
46 class WXDLLIMPEXP_BASE wxWindow
;
47 class WXDLLIMPEXP_BASE wxWindowList
;
48 class WXDLLIMPEXP_BASE wxPoint
;
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
55 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
57 // ----------------------------------------------------------------------------
58 // String functions (deprecated, use wxString)
59 // ----------------------------------------------------------------------------
61 // Make a copy of this string using 'new'
62 #if WXWIN_COMPATIBILITY_2_4
63 WXDLLIMPEXP_BASE wxChar
* copystring(const wxChar
*s
);
66 // A shorter way of using strcmp
67 #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
69 // ----------------------------------------------------------------------------
70 // Miscellaneous functions
71 // ----------------------------------------------------------------------------
74 WXDLLIMPEXP_BASE
void wxBell();
76 // Get OS description as a user-readable string
77 WXDLLIMPEXP_BASE wxString
wxGetOsDescription();
80 WXDLLIMPEXP_BASE
int wxGetOsVersion(int *majorVsn
= (int *) NULL
,
81 int *minorVsn
= (int *) NULL
);
83 // Return a string with the current date/time
84 WXDLLIMPEXP_BASE wxString
wxNow();
86 // Return path where wxWindows is installed (mostly useful in Unices)
87 WXDLLIMPEXP_BASE
const wxChar
*wxGetInstallPrefix();
88 // Return path to wxWin data (/usr/share/wx/%{version}) (Unices)
89 WXDLLIMPEXP_BASE wxString
wxGetDataDir();
93 // Don't synthesize KeyUp events holding down a key and producing
94 // KeyDown events with autorepeat. On by default and always on
96 WXDLLEXPORT
bool wxSetDetectableAutoRepeat( bool flag
);
98 // ----------------------------------------------------------------------------
99 // Window ID management
100 // ----------------------------------------------------------------------------
102 // Generate a unique ID
103 WXDLLEXPORT
long wxNewId();
105 // Ensure subsequent IDs don't clash with this one
106 WXDLLEXPORT
void wxRegisterId(long id
);
108 // Return the current ID
109 WXDLLEXPORT
long wxGetCurrentId();
113 // ----------------------------------------------------------------------------
114 // Various conversions
115 // ----------------------------------------------------------------------------
117 // these functions are deprecated, use wxString methods instead!
118 #if WXWIN_COMPATIBILITY_2_4
120 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxFloatToStringStr
;
121 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxDoubleToStringStr
;
123 WXDLLIMPEXP_BASE
void StringToFloat(const wxChar
*s
, float *number
);
124 WXDLLIMPEXP_BASE wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
);
125 WXDLLIMPEXP_BASE
void StringToDouble(const wxChar
*s
, double *number
);
126 WXDLLIMPEXP_BASE wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
);
127 WXDLLIMPEXP_BASE
void StringToInt(const wxChar
*s
, int *number
);
128 WXDLLIMPEXP_BASE
void StringToLong(const wxChar
*s
, long *number
);
129 WXDLLIMPEXP_BASE wxChar
* IntToString(int number
);
130 WXDLLIMPEXP_BASE wxChar
* LongToString(long number
);
132 #endif // WXWIN_COMPATIBILITY_2_4
134 // Convert 2-digit hex number to decimal
135 WXDLLIMPEXP_BASE
int wxHexToDec(const wxString
& buf
);
137 // Convert decimal integer to 2-character hex string
138 WXDLLIMPEXP_BASE
void wxDecToHex(int dec
, wxChar
*buf
);
139 WXDLLIMPEXP_BASE wxString
wxDecToHex(int dec
);
141 // ----------------------------------------------------------------------------
142 // Process management
143 // ----------------------------------------------------------------------------
145 // NB: for backwars compatibility reasons the values of wxEXEC_[A]SYNC *must*
146 // be 0 and 1, don't change!
150 // execute the process asynchronously
153 // execute it synchronously, i.e. wait until it finishes
156 // under Windows, don't hide the child even if it's IO is redirected (this
157 // is done by default)
160 // under Unix, if the process is the group leader then killing -pid kills
161 // all children as well as pid
162 wxEXEC_MAKE_GROUP_LEADER
= 4
165 // Execute another program.
167 // If flags contain wxEXEC_SYNC, return -1 on failure and the exit code of the
168 // process if everything was ok. Otherwise (i.e. if wxEXEC_ASYNC), return 0 on
169 // failure and the PID of the launched process if ok.
170 WXDLLIMPEXP_BASE
long wxExecute(wxChar
**argv
, int flags
= wxEXEC_ASYNC
,
171 wxProcess
*process
= (wxProcess
*) NULL
);
172 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
, int flags
= wxEXEC_ASYNC
,
173 wxProcess
*process
= (wxProcess
*) NULL
);
175 // execute the command capturing its output into an array line by line, this is
176 // always synchronous
177 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
178 wxArrayString
& output
);
180 // also capture stderr (also synchronous)
181 WXDLLIMPEXP_BASE
long wxExecute(const wxString
& command
,
182 wxArrayString
& output
,
183 wxArrayString
& error
);
187 wxSIGNONE
= 0, // verify if the process exists under Unix
194 wxSIGIOT
= wxSIGABRT
, // another name
205 // further signals are different in meaning between different Unix systems
210 wxKILL_OK
, // no error
211 wxKILL_BAD_SIGNAL
, // no such signal
212 wxKILL_ACCESS_DENIED
, // permission denied
213 wxKILL_NO_PROCESS
, // no such process
214 wxKILL_ERROR
// another, unspecified error
219 wxSHUTDOWN_POWEROFF
, // power off the computer
220 wxSHUTDOWN_REBOOT
// shutdown and reboot
223 // Shutdown or reboot the PC
224 WXDLLIMPEXP_BASE
bool wxShutdown(wxShutdownFlags wFlags
);
226 // send the given signal to the process (only NONE and KILL are supported under
227 // Windows, all others mean TERM), return 0 if ok and -1 on error
229 // return detailed error in rc if not NULL
230 WXDLLIMPEXP_BASE
int wxKill(long pid
,
231 wxSignal sig
= wxSIGTERM
,
232 wxKillError
*rc
= NULL
);
234 // Execute a command in an interactive shell window (always synchronously)
235 // If no command then just the shell
236 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
= wxEmptyString
);
238 // As wxShell(), but must give a (non interactive) command and its output will
239 // be returned in output array
240 WXDLLIMPEXP_BASE
bool wxShell(const wxString
& command
, wxArrayString
& output
);
242 // Sleep for nSecs seconds
243 WXDLLIMPEXP_BASE
void wxSleep(int nSecs
);
245 // Sleep for a given amount of milliseconds
246 WXDLLIMPEXP_BASE
void wxUsleep(unsigned long milliseconds
);
248 // Get the process id of the current process
249 WXDLLIMPEXP_BASE
unsigned long wxGetProcessId();
251 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
252 WXDLLIMPEXP_BASE
long wxGetFreeMemory();
254 // should wxApp::OnFatalException() be called?
255 WXDLLIMPEXP_BASE
bool wxHandleFatalExceptions(bool doit
= TRUE
);
257 // ----------------------------------------------------------------------------
258 // Environment variables
259 // ----------------------------------------------------------------------------
261 // returns TRUE if variable exists (value may be NULL if you just want to check
263 WXDLLIMPEXP_BASE
bool wxGetEnv(const wxString
& var
, wxString
*value
);
265 // set the env var name to the given value, return TRUE on success
266 WXDLLIMPEXP_BASE
bool wxSetEnv(const wxString
& var
, const wxChar
*value
);
268 // remove the env var from environment
269 inline bool wxUnsetEnv(const wxString
& var
) { return wxSetEnv(var
, NULL
); }
271 // ----------------------------------------------------------------------------
272 // Network and username functions.
273 // ----------------------------------------------------------------------------
275 // NB: "char *" functions are deprecated, use wxString ones!
278 WXDLLIMPEXP_BASE
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
279 WXDLLIMPEXP_BASE wxString
wxGetEmailAddress();
282 WXDLLIMPEXP_BASE
bool wxGetHostName(wxChar
*buf
, int maxSize
);
283 WXDLLIMPEXP_BASE wxString
wxGetHostName();
286 WXDLLIMPEXP_BASE wxString
wxGetFullHostName();
287 WXDLLIMPEXP_BASE
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
289 // Get user ID e.g. jacs (this is known as login name under Unix)
290 WXDLLIMPEXP_BASE
bool wxGetUserId(wxChar
*buf
, int maxSize
);
291 WXDLLIMPEXP_BASE wxString
wxGetUserId();
293 // Get user name e.g. Julian Smart
294 WXDLLIMPEXP_BASE
bool wxGetUserName(wxChar
*buf
, int maxSize
);
295 WXDLLIMPEXP_BASE wxString
wxGetUserName();
297 // Get current Home dir and copy to dest (returns pstr->c_str())
298 WXDLLIMPEXP_BASE wxString
wxGetHomeDir();
299 WXDLLIMPEXP_BASE
const wxChar
* wxGetHomeDir(wxString
*pstr
);
301 // Get the user's home dir (caller must copy --- volatile)
302 // returns NULL is no HOME dir is known
303 #if defined(__UNIX__) && wxUSE_UNICODE
304 WXDLLIMPEXP_BASE
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
306 WXDLLIMPEXP_BASE wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
309 // get number of total/free bytes on the disk where path belongs
310 WXDLLIMPEXP_BASE
bool wxGetDiskSpace(const wxString
& path
,
311 wxLongLong
*pTotal
= NULL
,
312 wxLongLong
*pFree
= NULL
);
314 #if wxUSE_GUI // GUI only things from now on
316 // ----------------------------------------------------------------------------
317 // Menu accelerators related things
318 // ----------------------------------------------------------------------------
320 WXDLLEXPORT wxChar
* wxStripMenuCodes(const wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
321 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
324 class WXDLLEXPORT wxAcceleratorEntry
;
325 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
326 #endif // wxUSE_ACCEL
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
332 // Returns menu item id or -1 if none.
333 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
335 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
336 // is always present but may be less reliable than a native version.
337 WXDLLEXPORT wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
);
338 WXDLLEXPORT wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
);
340 // NB: this function is obsolete, use wxWindow::FindWindowByLabel() instead
342 // Find the window/widget with the given title or label.
343 // Pass a parent to begin the search from, or NULL to look through
345 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
347 // NB: this function is obsolete, use wxWindow::FindWindowByName() instead
349 // Find window by name, and if that fails, by label.
350 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
352 // ----------------------------------------------------------------------------
353 // Message/event queue helpers
354 // ----------------------------------------------------------------------------
356 // Yield to other apps/messages and disable user input
357 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
, bool onlyIfNeeded
= FALSE
);
359 // Enable or disable input to all top level windows
360 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= TRUE
);
362 // Check whether this window wants to process messages, e.g. Stop button
363 // in long calculations.
364 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
366 // Consume all events until no more left
367 WXDLLEXPORT
void wxFlushEvents();
369 // a class which disables all windows (except, may be, thegiven one) in its
370 // ctor and enables them back in its dtor
371 class WXDLLEXPORT wxWindowDisabler
374 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
378 wxWindowList
*m_winDisabled
;
380 DECLARE_NO_COPY_CLASS(wxWindowDisabler
)
383 // ----------------------------------------------------------------------------
385 // ----------------------------------------------------------------------------
387 // Set the cursor to the busy cursor for all windows
388 class WXDLLEXPORT wxCursor
;
389 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
390 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
392 // Restore cursor to normal
393 WXDLLEXPORT
void wxEndBusyCursor();
395 // TRUE if we're between the above two calls
396 WXDLLEXPORT
bool wxIsBusy();
398 // Convenience class so we can just create a wxBusyCursor object on the stack
399 class WXDLLEXPORT wxBusyCursor
402 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
403 { wxBeginBusyCursor(cursor
); }
405 { wxEndBusyCursor(); }
407 // FIXME: These two methods are currently only implemented (and needed?)
408 // in wxGTK. BusyCursor handling should probably be moved to
409 // common code since the wxGTK and wxMSW implementations are very
410 // similar except for wxMSW using HCURSOR directly instead of
412 static const wxCursor
&GetStoredCursor();
413 static const wxCursor
GetBusyCursor();
417 // ----------------------------------------------------------------------------
418 // Reading and writing resources (eg WIN.INI, .Xdefaults)
419 // ----------------------------------------------------------------------------
422 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
423 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
424 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
425 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
427 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
428 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
429 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
430 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
431 #endif // wxUSE_RESOURCES
433 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
435 // MSW only: get user-defined resource from the .res file.
436 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
438 WXDLLEXPORT
extern const wxChar
* wxUserResourceStr
;
439 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
442 // ----------------------------------------------------------------------------
443 // Display and colorss (X only)
444 // ----------------------------------------------------------------------------
447 void *wxGetDisplay();
451 WXDisplay
*wxGetDisplay();
452 bool wxSetDisplay(const wxString
& display_name
);
453 wxString
wxGetDisplayName();
458 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
459 // The resulting warnings are switched off here
460 #pragma message disable nosimpint
462 // #include <X11/Xlib.h>
464 #pragma message enable nosimpint
471 // ----------------------------------------------------------------------------
472 // wxYield(): these functions are obsolete, please use wxApp methods instead!
473 // ----------------------------------------------------------------------------
475 // Yield to other apps/messages
476 WXDLLIMPEXP_BASE
bool wxYield();
478 // Like wxYield, but fails silently if the yield is recursive.
479 WXDLLIMPEXP_BASE
bool wxYieldIfNeeded();
481 // ----------------------------------------------------------------------------
482 // Error message functions used by wxWindows (deprecated, use wxLog)
483 // ----------------------------------------------------------------------------
485 #if WXWIN_COMPATIBILITY_2_2
487 // Format a message on the standard error (UNIX) or the debugging
489 WXDLLIMPEXP_BASE
void wxDebugMsg(const wxChar
*fmt
...) ATTRIBUTE_PRINTF_1
;
491 // Non-fatal error (continues)
492 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxInternalErrorStr
;
493 WXDLLIMPEXP_BASE
void wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
495 // Fatal error (exits)
496 WXDLLIMPEXP_DATA_BASE(extern const wxChar
*) wxFatalErrorStr
;
497 WXDLLIMPEXP_BASE
void wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);
499 #endif // WXWIN_COMPATIBILITY_2_2