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 WXDLLEXPORT wxProcess
;
43 class WXDLLEXPORT wxFrame
;
44 class WXDLLEXPORT wxWindow
;
45 class WXDLLEXPORT wxWindowList
;
46 class WXDLLEXPORT 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 // Useful buffer (FIXME VZ: To be removed!!!)
60 // Now only needed in MSW port
61 #if !defined(__WXMOTIF__) && !defined(__WXGTK__) && !defined(__WXX11__) && !defined(__WXMGL__) && !defined(__WXMAC__)
62 WXDLLEXPORT_DATA(extern wxChar
*) wxBuffer
;
65 // Make a copy of this string using 'new'
66 WXDLLEXPORT wxChar
* copystring(const wxChar
*s
);
68 #if WXWIN_COMPATIBILITY_2
69 // Matches string one within string two regardless of case
70 WXDLLEXPORT
bool StringMatch(const wxChar
*one
, const wxChar
*two
, bool subString
= TRUE
, bool exact
= FALSE
);
73 // A shorter way of using strcmp
74 #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
76 // ----------------------------------------------------------------------------
77 // Miscellaneous functions
78 // ----------------------------------------------------------------------------
81 WXDLLEXPORT
void wxBell();
83 // Get OS description as a user-readable string
84 WXDLLEXPORT wxString
wxGetOsDescription();
87 WXDLLEXPORT
int wxGetOsVersion(int *majorVsn
= (int *) NULL
,
88 int *minorVsn
= (int *) NULL
);
90 // Return a string with the current date/time
91 WXDLLEXPORT wxString
wxNow();
93 // Return path where wxWindows is installed (mostly useful in Unices)
94 WXDLLEXPORT
const wxChar
*wxGetInstallPrefix();
95 // Return path to wxWin data (/usr/share/wx/%{version}) (Unices)
96 WXDLLEXPORT wxString
wxGetDataDir();
100 // Don't synthesize KeyUp events holding down a key and producing
101 // KeyDown events with autorepeat. On by default and always on
103 WXDLLEXPORT
bool wxSetDetectableAutoRepeat( bool flag
);
105 // ----------------------------------------------------------------------------
106 // Window ID management
107 // ----------------------------------------------------------------------------
109 // Generate a unique ID
110 WXDLLEXPORT
long wxNewId();
111 #if !defined(NewId) && defined(WXWIN_COMPATIBILITY)
112 #define NewId wxNewId
115 // Ensure subsequent IDs don't clash with this one
116 WXDLLEXPORT
void wxRegisterId(long id
);
117 #if !defined(RegisterId) && defined(WXWIN_COMPATIBILITY)
118 #define RegisterId wxRegisterId
121 // Return the current ID
122 WXDLLEXPORT
long wxGetCurrentId();
126 // ----------------------------------------------------------------------------
127 // Various conversions
128 // ----------------------------------------------------------------------------
130 // these functions are deprecated, use wxString methods instead!
131 #if WXWIN_COMPATIBILITY_2_4
133 WXDLLEXPORT_DATA(extern const wxChar
*) wxFloatToStringStr
;
134 WXDLLEXPORT_DATA(extern const wxChar
*) wxDoubleToStringStr
;
136 WXDLLEXPORT
void StringToFloat(const wxChar
*s
, float *number
);
137 WXDLLEXPORT wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
);
138 WXDLLEXPORT
void StringToDouble(const wxChar
*s
, double *number
);
139 WXDLLEXPORT wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
);
140 WXDLLEXPORT
void StringToInt(const wxChar
*s
, int *number
);
141 WXDLLEXPORT
void StringToLong(const wxChar
*s
, long *number
);
142 WXDLLEXPORT wxChar
* IntToString(int number
);
143 WXDLLEXPORT wxChar
* LongToString(long number
);
145 #endif // WXWIN_COMPATIBILITY_2_4
147 // Convert 2-digit hex number to decimal
148 WXDLLEXPORT
int wxHexToDec(const wxString
& buf
);
150 // Convert decimal integer to 2-character hex string
151 WXDLLEXPORT
void wxDecToHex(int dec
, wxChar
*buf
);
152 WXDLLEXPORT wxString
wxDecToHex(int dec
);
154 // ----------------------------------------------------------------------------
155 // Process management
156 // ----------------------------------------------------------------------------
158 // NB: for backwars compatibility reasons the values of wxEXEC_[A]SYNC *must*
159 // be 0 and 1, don't change!
163 // execute the process asynchronously
166 // execute it synchronously, i.e. wait until it finishes
169 // under Windows, don't hide the child even if it's IO is redirected (this
170 // is done by default)
173 // under Unix, if the process is the group leader then killing -pid kills
174 // all children as well as pid
175 wxEXEC_MAKE_GROUP_LEADER
= 4
178 // Execute another program.
180 // If flags contain wxEXEC_SYNC, return -1 on failure and the exit code of the
181 // process if everything was ok. Otherwise (i.e. if wxEXEC_ASYNC), return 0 on
182 // failure and the PID of the launched process if ok.
183 WXDLLEXPORT
long wxExecute(wxChar
**argv
, int flags
= wxEXEC_ASYNC
,
184 wxProcess
*process
= (wxProcess
*) NULL
);
185 WXDLLEXPORT
long wxExecute(const wxString
& command
, int flags
= wxEXEC_ASYNC
,
186 wxProcess
*process
= (wxProcess
*) NULL
);
188 // execute the command capturing its output into an array line by line, this is
189 // always synchronous
190 WXDLLEXPORT
long wxExecute(const wxString
& command
,
191 wxArrayString
& output
);
193 // also capture stderr (also synchronous)
194 WXDLLEXPORT
long wxExecute(const wxString
& command
,
195 wxArrayString
& output
,
196 wxArrayString
& error
);
200 wxSIGNONE
= 0, // verify if the process exists under Unix
207 wxSIGIOT
= wxSIGABRT
, // another name
218 // further signals are different in meaning between different Unix systems
223 wxKILL_OK
, // no error
224 wxKILL_BAD_SIGNAL
, // no such signal
225 wxKILL_ACCESS_DENIED
, // permission denied
226 wxKILL_NO_PROCESS
, // no such process
227 wxKILL_ERROR
// another, unspecified error
232 wxSHUTDOWN_POWEROFF
, // power off the computer
233 wxSHUTDOWN_REBOOT
// shutdown and reboot
236 // Shutdown or reboot the PC
237 WXDLLEXPORT
bool wxShutdown(wxShutdownFlags wFlags
);
239 // send the given signal to the process (only NONE and KILL are supported under
240 // Windows, all others mean TERM), return 0 if ok and -1 on error
242 // return detailed error in rc if not NULL
243 WXDLLEXPORT
int wxKill(long pid
,
244 wxSignal sig
= wxSIGTERM
,
245 wxKillError
*rc
= NULL
);
247 // Execute a command in an interactive shell window (always synchronously)
248 // If no command then just the shell
249 WXDLLEXPORT
bool wxShell(const wxString
& command
= wxEmptyString
);
251 // As wxShell(), but must give a (non interactive) command and its output will
252 // be returned in output array
253 WXDLLEXPORT
bool wxShell(const wxString
& command
, wxArrayString
& output
);
255 // Sleep for nSecs seconds
256 WXDLLEXPORT
void wxSleep(int nSecs
);
258 // Sleep for a given amount of milliseconds
259 WXDLLEXPORT
void wxUsleep(unsigned long milliseconds
);
261 // Get the process id of the current process
262 WXDLLEXPORT
unsigned long wxGetProcessId();
264 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
265 WXDLLEXPORT
long wxGetFreeMemory();
267 // should wxApp::OnFatalException() be called?
268 WXDLLEXPORT
bool wxHandleFatalExceptions(bool doit
= TRUE
);
270 // ----------------------------------------------------------------------------
271 // Environment variables
272 // ----------------------------------------------------------------------------
274 // returns TRUE if variable exists (value may be NULL if you just want to check
276 WXDLLEXPORT
bool wxGetEnv(const wxString
& var
, wxString
*value
);
278 // set the env var name to the given value, return TRUE on success
279 WXDLLEXPORT
bool wxSetEnv(const wxString
& var
, const wxChar
*value
);
281 // remove the env var from environment
282 inline bool wxUnsetEnv(const wxString
& var
) { return wxSetEnv(var
, NULL
); }
284 // ----------------------------------------------------------------------------
285 // Network and username functions.
286 // ----------------------------------------------------------------------------
288 // NB: "char *" functions are deprecated, use wxString ones!
291 WXDLLEXPORT
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
292 WXDLLEXPORT wxString
wxGetEmailAddress();
295 WXDLLEXPORT
bool wxGetHostName(wxChar
*buf
, int maxSize
);
296 WXDLLEXPORT wxString
wxGetHostName();
299 WXDLLEXPORT wxString
wxGetFullHostName();
300 WXDLLEXPORT
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
302 // Get user ID e.g. jacs (this is known as login name under Unix)
303 WXDLLEXPORT
bool wxGetUserId(wxChar
*buf
, int maxSize
);
304 WXDLLEXPORT wxString
wxGetUserId();
306 // Get user name e.g. Julian Smart
307 WXDLLEXPORT
bool wxGetUserName(wxChar
*buf
, int maxSize
);
308 WXDLLEXPORT wxString
wxGetUserName();
310 // Get current Home dir and copy to dest (returns pstr->c_str())
311 WXDLLEXPORT wxString
wxGetHomeDir();
312 WXDLLEXPORT
const wxChar
* wxGetHomeDir(wxString
*pstr
);
314 // Get the user's home dir (caller must copy --- volatile)
315 // returns NULL is no HOME dir is known
316 #if defined(__UNIX__) && wxUSE_UNICODE
317 WXDLLEXPORT
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
319 WXDLLEXPORT wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
322 // get number of total/free bytes on the disk where path belongs
323 WXDLLEXPORT
bool wxGetDiskSpace(const wxString
& path
,
324 wxLongLong
*pTotal
= NULL
,
325 wxLongLong
*pFree
= NULL
);
327 #if wxUSE_GUI // GUI only things from now on
329 // ----------------------------------------------------------------------------
330 // Menu accelerators related things
331 // ----------------------------------------------------------------------------
333 WXDLLEXPORT wxChar
* wxStripMenuCodes(const wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
334 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
337 class WXDLLEXPORT wxAcceleratorEntry
;
338 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
339 #endif // wxUSE_ACCEL
341 // ----------------------------------------------------------------------------
343 // ----------------------------------------------------------------------------
345 // Returns menu item id or -1 if none.
346 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
348 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
349 // is always present but may be less reliable than a native version.
350 WXDLLEXPORT wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
);
351 WXDLLEXPORT wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
);
353 // NB: this function is obsolete, use wxWindow::FindWindowByLabel() instead
355 // Find the window/widget with the given title or label.
356 // Pass a parent to begin the search from, or NULL to look through
358 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
360 // NB: this function is obsolete, use wxWindow::FindWindowByName() instead
362 // Find window by name, and if that fails, by label.
363 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
365 // ----------------------------------------------------------------------------
366 // Message/event queue helpers
367 // ----------------------------------------------------------------------------
369 // NB: these functions are obsolete, please use wxApp methods instead!
371 // Yield to other apps/messages
372 WXDLLEXPORT
bool wxYield();
374 // Like wxYield, but fails silently if the yield is recursive.
375 WXDLLEXPORT
bool wxYieldIfNeeded();
377 // Yield to other apps/messages and disable user input
378 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
, bool onlyIfNeeded
= FALSE
);
380 // Enable or disable input to all top level windows
381 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= TRUE
);
383 // Check whether this window wants to process messages, e.g. Stop button
384 // in long calculations.
385 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
387 // Consume all events until no more left
388 WXDLLEXPORT
void wxFlushEvents();
390 // a class which disables all windows (except, may be, thegiven one) in its
391 // ctor and enables them back in its dtor
392 class WXDLLEXPORT wxWindowDisabler
395 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
399 wxWindowList
*m_winDisabled
;
401 DECLARE_NO_COPY_CLASS(wxWindowDisabler
)
404 // ----------------------------------------------------------------------------
406 // ----------------------------------------------------------------------------
408 // Set the cursor to the busy cursor for all windows
409 class WXDLLEXPORT wxCursor
;
410 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
411 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
413 // Restore cursor to normal
414 WXDLLEXPORT
void wxEndBusyCursor();
416 // TRUE if we're between the above two calls
417 WXDLLEXPORT
bool wxIsBusy();
419 // Convenience class so we can just create a wxBusyCursor object on the stack
420 class WXDLLEXPORT wxBusyCursor
423 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
424 { wxBeginBusyCursor(cursor
); }
426 { wxEndBusyCursor(); }
428 // FIXME: These two methods are currently only implemented (and needed?)
429 // in wxGTK. BusyCursor handling should probably be moved to
430 // common code since the wxGTK and wxMSW implementations are very
431 // similar except for wxMSW using HCURSOR directly instead of
433 static const wxCursor
&GetStoredCursor();
434 static const wxCursor
GetBusyCursor();
438 // ----------------------------------------------------------------------------
439 // Reading and writing resources (eg WIN.INI, .Xdefaults)
440 // ----------------------------------------------------------------------------
443 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
444 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
445 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
446 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
448 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
449 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
450 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
451 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
452 #endif // wxUSE_RESOURCES
454 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
456 // MSW only: get user-defined resource from the .res file.
457 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
459 WXDLLEXPORT
extern const wxChar
* wxUserResourceStr
;
460 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
463 // ----------------------------------------------------------------------------
464 // Display and colorss (X only)
465 // ----------------------------------------------------------------------------
468 void *wxGetDisplay();
472 WXDisplay
*wxGetDisplay();
473 bool wxSetDisplay(const wxString
& display_name
);
474 wxString
wxGetDisplayName();
479 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
480 // The resulting warnings are switched off here
481 #pragma message disable nosimpint
483 // #include <X11/Xlib.h>
485 #pragma message enable nosimpint
492 // ----------------------------------------------------------------------------
493 // Error message functions used by wxWindows (deprecated, use wxLog)
494 // ----------------------------------------------------------------------------
496 #if WXWIN_COMPATIBILITY_2_2
498 // Format a message on the standard error (UNIX) or the debugging
500 WXDLLEXPORT
void wxDebugMsg(const wxChar
*fmt
...) ATTRIBUTE_PRINTF_1
;
502 // Non-fatal error (continues)
503 WXDLLEXPORT_DATA(extern const wxChar
*) wxInternalErrorStr
;
504 WXDLLEXPORT
void wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
506 // Fatal error (exits)
507 WXDLLEXPORT_DATA(extern const wxChar
*) wxFatalErrorStr
;
508 WXDLLEXPORT
void wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);
510 #endif // WXWIN_COMPATIBILITY_2_2