1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Miscellaneous utilities
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
20 #pragma interface "utils.h"
24 #include "wx/object.h"
26 #include "wx/filefn.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 WXDLLEXPORT wxProcess
;
44 class WXDLLEXPORT wxFrame
;
45 class WXDLLEXPORT wxWindow
;
46 class WXDLLEXPORT wxWindowList
;
47 class WXDLLEXPORT 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 // Useful buffer (FIXME VZ: To be removed!!!)
61 WXDLLEXPORT_DATA(extern wxChar
*) wxBuffer
;
63 // Make a copy of this string using 'new'
64 WXDLLEXPORT wxChar
* copystring(const wxChar
*s
);
66 // Matches string one within string two regardless of case
67 WXDLLEXPORT
bool StringMatch(wxChar
*one
, 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 WXDLLEXPORT
void wxBell();
79 // Get OS description as a user-readable string
80 WXDLLEXPORT wxString
wxGetOsDescription();
83 WXDLLEXPORT
int wxGetOsVersion(int *majorVsn
= (int *) NULL
,
84 int *minorVsn
= (int *) NULL
);
86 // Return a string with the current date/time
87 WXDLLEXPORT wxString
wxNow();
90 // Don't synthesize KeyUp events holding down a key and producing
91 // KeyDown events with autorepeat. On by default and always on
93 WXDLLEXPORT
bool wxSetDetectableAutoRepeat( bool flag
);
95 // ----------------------------------------------------------------------------
96 // Window ID management
97 // ----------------------------------------------------------------------------
99 // Generate a unique ID
100 WXDLLEXPORT
long wxNewId();
101 #if !defined(NewId) && defined(WXWIN_COMPATIBILITY)
102 #define NewId wxNewId
105 // Ensure subsequent IDs don't clash with this one
106 WXDLLEXPORT
void wxRegisterId(long id
);
107 #if !defined(RegisterId) && defined(WXWIN_COMPATIBILITY)
108 #define RegisterId wxRegisterId
111 // Return the current ID
112 WXDLLEXPORT
long wxGetCurrentId();
116 // ----------------------------------------------------------------------------
117 // Various conversions
118 // ----------------------------------------------------------------------------
120 WXDLLEXPORT_DATA(extern const wxChar
*) wxFloatToStringStr
;
121 WXDLLEXPORT_DATA(extern const wxChar
*) wxDoubleToStringStr
;
123 WXDLLEXPORT
void StringToFloat(wxChar
*s
, float *number
);
124 WXDLLEXPORT wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
);
125 WXDLLEXPORT
void StringToDouble(wxChar
*s
, double *number
);
126 WXDLLEXPORT wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
);
127 WXDLLEXPORT
void StringToInt(wxChar
*s
, int *number
);
128 WXDLLEXPORT
void StringToLong(wxChar
*s
, long *number
);
129 WXDLLEXPORT wxChar
* IntToString(int number
);
130 WXDLLEXPORT wxChar
* LongToString(long number
);
132 // Convert 2-digit hex number to decimal
133 WXDLLEXPORT
int wxHexToDec(const wxString
& buf
);
135 // Convert decimal integer to 2-character hex string
136 WXDLLEXPORT
void wxDecToHex(int dec
, wxChar
*buf
);
137 WXDLLEXPORT wxString
wxDecToHex(int dec
);
139 // ----------------------------------------------------------------------------
140 // Process management
141 // ----------------------------------------------------------------------------
143 // Execute another program. Returns 0 if there was an error, a PID otherwise.
144 WXDLLEXPORT
long wxExecute(wxChar
**argv
, bool sync
= FALSE
,
145 wxProcess
*process
= (wxProcess
*) NULL
);
146 WXDLLEXPORT
long wxExecute(const wxString
& command
, bool sync
= FALSE
,
147 wxProcess
*process
= (wxProcess
*) NULL
);
149 // execute the command capturing its output into an array line by line
150 WXDLLEXPORT
long wxExecute(const wxString
& command
,
151 wxArrayString
& output
);
153 // also capture stderr
154 WXDLLEXPORT
long wxExecute(const wxString
& command
,
155 wxArrayString
& output
,
156 wxArrayString
& error
);
160 wxSIGNONE
= 0, // verify if the process exists under Unix
167 wxSIGIOT
= wxSIGABRT
, // another name
178 // further signals are different in meaning between different Unix systems
183 wxKILL_OK
, // no error
184 wxKILL_BAD_SIGNAL
, // no such signal
185 wxKILL_ACCESS_DENIED
, // permission denied
186 wxKILL_NO_PROCESS
, // no such process
187 wxKILL_ERROR
// another, unspecified error
190 // send the given signal to the process (only NONE and KILL are supported under
191 // Windows, all others mean TERM), return 0 if ok and -1 on error
193 // return detailed error in rc if not NULL
194 WXDLLEXPORT
int wxKill(long pid
,
195 wxSignal sig
= wxSIGTERM
,
196 wxKillError
*rc
= NULL
);
198 // Execute a command in an interactive shell window (always synchronously)
199 // If no command then just the shell
200 WXDLLEXPORT
bool wxShell(const wxString
& command
= wxEmptyString
);
202 // As wxShell(), but must give a (non interactive) command and its output will
203 // be returned in output array
204 WXDLLEXPORT
bool wxShell(const wxString
& command
, wxArrayString
& output
);
206 // Sleep for nSecs seconds
207 WXDLLEXPORT
void wxSleep(int nSecs
);
209 // Sleep for a given amount of milliseconds
210 WXDLLEXPORT
void wxUsleep(unsigned long milliseconds
);
212 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
213 WXDLLEXPORT
long wxGetFreeMemory();
215 // should wxApp::OnFatalException() be called?
216 WXDLLEXPORT
bool wxHandleFatalExceptions(bool doit
= TRUE
);
218 // ----------------------------------------------------------------------------
219 // Environment variables
220 // ----------------------------------------------------------------------------
222 // returns TRUE if variable exists (value may be NULL if you just want to check
224 WXDLLEXPORT
bool wxGetEnv(const wxString
& var
, wxString
*value
);
226 // set the env var name to the given value, return TRUE on success
227 WXDLLEXPORT
bool wxSetEnv(const wxString
& var
, const wxChar
*value
);
229 // remove the env var from environment
230 inline bool wxUnsetEnv(const wxString
& var
) { return wxSetEnv(var
, NULL
); }
232 // ----------------------------------------------------------------------------
233 // Network and username functions.
234 // ----------------------------------------------------------------------------
236 // NB: "char *" functions are deprecated, use wxString ones!
239 WXDLLEXPORT
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
240 WXDLLEXPORT wxString
wxGetEmailAddress();
243 WXDLLEXPORT
bool wxGetHostName(wxChar
*buf
, int maxSize
);
244 WXDLLEXPORT wxString
wxGetHostName();
247 WXDLLEXPORT wxString
wxGetFullHostName();
248 WXDLLEXPORT
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
250 // Get user ID e.g. jacs (this is known as login name under Unix)
251 WXDLLEXPORT
bool wxGetUserId(wxChar
*buf
, int maxSize
);
252 WXDLLEXPORT wxString
wxGetUserId();
254 // Get user name e.g. Julian Smart
255 WXDLLEXPORT
bool wxGetUserName(wxChar
*buf
, int maxSize
);
256 WXDLLEXPORT wxString
wxGetUserName();
258 // Get current Home dir and copy to dest (returns pstr->c_str())
259 WXDLLEXPORT wxString
wxGetHomeDir();
260 WXDLLEXPORT
const wxChar
* wxGetHomeDir(wxString
*pstr
);
262 // Get the user's home dir (caller must copy --- volatile)
263 // returns NULL is no HOME dir is known
264 #if defined(__UNIX__) && wxUSE_UNICODE
265 WXDLLEXPORT
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
267 WXDLLEXPORT wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
271 WXDLLEXPORT wxString
wxMacFindFolder(short vRefNum
,
273 Boolean createFolder
);
276 // get number of total/free bytes on the disk where path belongs
277 WXDLLEXPORT
bool wxGetDiskSpace(const wxString
& path
,
278 wxLongLong
*pTotal
= NULL
,
279 wxLongLong
*pFree
= NULL
);
281 #if wxUSE_GUI // GUI only things from now on
283 // ----------------------------------------------------------------------------
284 // Menu accelerators related things
285 // ----------------------------------------------------------------------------
287 WXDLLEXPORT wxChar
* wxStripMenuCodes(wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
288 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
291 class WXDLLEXPORT wxAcceleratorEntry
;
292 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
293 #endif // wxUSE_ACCEL
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
299 // Find the window/widget with the given title or label.
300 // Pass a parent to begin the search from, or NULL to look through
302 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
304 // Find window by name, and if that fails, by label.
305 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
307 // Returns menu item id or -1 if none.
308 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
310 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
311 // is always present but may be less reliable than a native version.
312 WXDLLEXPORT wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
);
313 WXDLLEXPORT wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
);
315 // ----------------------------------------------------------------------------
316 // Message/event queue helpers
317 // ----------------------------------------------------------------------------
319 // Yield to other apps/messages
320 WXDLLEXPORT
bool wxYield();
322 // Like wxYield, but fails silently if the yield is recursive.
323 WXDLLEXPORT
bool wxYieldIfNeeded();
325 // Yield to other apps/messages and disable user input
326 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
);
328 // Enable or disable input to all top level windows
329 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= TRUE
);
331 // Check whether this window wants to process messages, e.g. Stop button
332 // in long calculations.
333 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
335 // Consume all events until no more left
336 WXDLLEXPORT
void wxFlushEvents();
338 // a class which disables all windows (except, may be, thegiven one) in its
339 // ctor and enables them back in its dtor
340 class WXDLLEXPORT wxWindowDisabler
343 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
347 wxWindowList
*m_winDisabled
;
350 // ----------------------------------------------------------------------------
352 // ----------------------------------------------------------------------------
354 // Set the cursor to the busy cursor for all windows
355 class WXDLLEXPORT wxCursor
;
356 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
357 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
359 // Restore cursor to normal
360 WXDLLEXPORT
void wxEndBusyCursor();
362 // TRUE if we're between the above two calls
363 WXDLLEXPORT
bool wxIsBusy();
365 // Convenience class so we can just create a wxBusyCursor object on the stack
366 class WXDLLEXPORT wxBusyCursor
369 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
370 { wxBeginBusyCursor(cursor
); }
372 { wxEndBusyCursor(); }
374 // FIXME: These two methods are currently only implemented (and needed?)
375 // in wxGTK. BusyCursor handling should probably be moved to
376 // common code since the wxGTK and wxMSW implementations are very
377 // similar except for wxMSW using HCURSOR directly instead of
379 static const wxCursor
&GetStoredCursor();
380 static const wxCursor
GetBusyCursor();
384 // ----------------------------------------------------------------------------
385 // Reading and writing resources (eg WIN.INI, .Xdefaults)
386 // ----------------------------------------------------------------------------
389 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
390 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
391 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
392 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
394 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
395 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
396 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
397 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
398 #endif // wxUSE_RESOURCES
400 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
402 // MSW only: get user-defined resource from the .res file.
403 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
405 WXDLLEXPORT
extern const wxChar
* wxUserResourceStr
;
406 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
408 // Implemented in utils.cpp: VC++, Win95 only. Sets up a console for standard
410 WXDLLEXPORT
void wxRedirectIOToConsole();
414 // ----------------------------------------------------------------------------
415 // Display and colorss (X only)
416 // ----------------------------------------------------------------------------
419 void *wxGetDisplay();
423 WXDisplay
*wxGetDisplay();
424 bool wxSetDisplay(const wxString
& display_name
);
425 wxString
wxGetDisplayName();
430 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
431 // The resulting warnings are switched off here
432 #pragma message disable nosimpint
434 #include <X11/Xlib.h>
436 #pragma message enable nosimpint
439 #define wxMAX_RGB 0xff
440 #define wxMAX_SV 1000
441 #define wxSIGN(x) ((x < 0) ? -x : x)
446 typedef struct wx_hsv
{
450 #define wxMax3(x,y,z) ((x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z))
451 #define wxMin3(x,y,z) ((x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z))
453 void wxHSVToXColor(wxHSV
*hsv
,XColor
*xcolor
);
454 void wxXColorToHSV(wxHSV
*hsv
,XColor
*xcolor
);
455 void wxAllocNearestColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
456 void wxAllocColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
462 // ----------------------------------------------------------------------------
463 // Error message functions used by wxWindows (deprecated, use wxLog)
464 // ----------------------------------------------------------------------------
466 // Format a message on the standard error (UNIX) or the debugging
468 WXDLLEXPORT
void wxDebugMsg(const wxChar
*fmt
...);
470 // Non-fatal error (continues)
471 WXDLLEXPORT_DATA(extern const wxChar
*) wxInternalErrorStr
;
472 WXDLLEXPORT
void wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
474 // Fatal error (exits)
475 WXDLLEXPORT_DATA(extern const wxChar
*) wxFatalErrorStr
;
476 WXDLLEXPORT
void wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);