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"
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 WXDLLEXPORT_DATA(extern wxChar
*) wxBuffer
;
62 // Make a copy of this string using 'new'
63 WXDLLEXPORT wxChar
* copystring(const wxChar
*s
);
65 // Matches string one within string two regardless of case
66 WXDLLEXPORT
bool StringMatch(wxChar
*one
, wxChar
*two
, bool subString
= TRUE
, bool exact
= FALSE
);
68 // A shorter way of using strcmp
69 #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
71 // ----------------------------------------------------------------------------
72 // Miscellaneous functions
73 // ----------------------------------------------------------------------------
76 WXDLLEXPORT
void wxBell();
78 // Get OS description as a user-readable string
79 WXDLLEXPORT wxString
wxGetOsDescription();
82 WXDLLEXPORT
int wxGetOsVersion(int *majorVsn
= (int *) NULL
,
83 int *minorVsn
= (int *) NULL
);
85 // Return a string with the current date/time
86 WXDLLEXPORT wxString
wxNow();
88 // Return path where wxWindows is installed (mostly useful in Unices)
89 WXDLLEXPORT
const wxChar
*wxGetInstallPrefix();
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();
104 #if !defined(NewId) && defined(WXWIN_COMPATIBILITY)
105 #define NewId wxNewId
108 // Ensure subsequent IDs don't clash with this one
109 WXDLLEXPORT
void wxRegisterId(long id
);
110 #if !defined(RegisterId) && defined(WXWIN_COMPATIBILITY)
111 #define RegisterId wxRegisterId
114 // Return the current ID
115 WXDLLEXPORT
long wxGetCurrentId();
119 // ----------------------------------------------------------------------------
120 // Various conversions
121 // ----------------------------------------------------------------------------
123 WXDLLEXPORT_DATA(extern const wxChar
*) wxFloatToStringStr
;
124 WXDLLEXPORT_DATA(extern const wxChar
*) wxDoubleToStringStr
;
126 WXDLLEXPORT
void StringToFloat(wxChar
*s
, float *number
);
127 WXDLLEXPORT wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
);
128 WXDLLEXPORT
void StringToDouble(wxChar
*s
, double *number
);
129 WXDLLEXPORT wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
);
130 WXDLLEXPORT
void StringToInt(wxChar
*s
, int *number
);
131 WXDLLEXPORT
void StringToLong(wxChar
*s
, long *number
);
132 WXDLLEXPORT wxChar
* IntToString(int number
);
133 WXDLLEXPORT wxChar
* LongToString(long number
);
135 // Convert 2-digit hex number to decimal
136 WXDLLEXPORT
int wxHexToDec(const wxString
& buf
);
138 // Convert decimal integer to 2-character hex string
139 WXDLLEXPORT
void wxDecToHex(int dec
, wxChar
*buf
);
140 WXDLLEXPORT wxString
wxDecToHex(int dec
);
142 // ----------------------------------------------------------------------------
143 // Process management
144 // ----------------------------------------------------------------------------
146 // Execute another program. Returns 0 if there was an error, a PID otherwise.
147 WXDLLEXPORT
long wxExecute(wxChar
**argv
, bool sync
= FALSE
,
148 wxProcess
*process
= (wxProcess
*) NULL
);
149 WXDLLEXPORT
long wxExecute(const wxString
& command
, bool sync
= FALSE
,
150 wxProcess
*process
= (wxProcess
*) NULL
);
152 // execute the command capturing its output into an array line by line
153 WXDLLEXPORT
long wxExecute(const wxString
& command
,
154 wxArrayString
& output
);
156 // also capture stderr
157 WXDLLEXPORT
long wxExecute(const wxString
& command
,
158 wxArrayString
& output
,
159 wxArrayString
& error
);
163 wxSIGNONE
= 0, // verify if the process exists under Unix
170 wxSIGIOT
= wxSIGABRT
, // another name
181 // further signals are different in meaning between different Unix systems
186 wxKILL_OK
, // no error
187 wxKILL_BAD_SIGNAL
, // no such signal
188 wxKILL_ACCESS_DENIED
, // permission denied
189 wxKILL_NO_PROCESS
, // no such process
190 wxKILL_ERROR
// another, unspecified error
193 // send the given signal to the process (only NONE and KILL are supported under
194 // Windows, all others mean TERM), return 0 if ok and -1 on error
196 // return detailed error in rc if not NULL
197 WXDLLEXPORT
int wxKill(long pid
,
198 wxSignal sig
= wxSIGTERM
,
199 wxKillError
*rc
= NULL
);
201 // Execute a command in an interactive shell window (always synchronously)
202 // If no command then just the shell
203 WXDLLEXPORT
bool wxShell(const wxString
& command
= wxEmptyString
);
205 // As wxShell(), but must give a (non interactive) command and its output will
206 // be returned in output array
207 WXDLLEXPORT
bool wxShell(const wxString
& command
, wxArrayString
& output
);
209 // Sleep for nSecs seconds
210 WXDLLEXPORT
void wxSleep(int nSecs
);
212 // Sleep for a given amount of milliseconds
213 WXDLLEXPORT
void wxUsleep(unsigned long milliseconds
);
215 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
216 WXDLLEXPORT
long wxGetFreeMemory();
218 // should wxApp::OnFatalException() be called?
219 WXDLLEXPORT
bool wxHandleFatalExceptions(bool doit
= TRUE
);
221 // ----------------------------------------------------------------------------
222 // Environment variables
223 // ----------------------------------------------------------------------------
225 // returns TRUE if variable exists (value may be NULL if you just want to check
227 WXDLLEXPORT
bool wxGetEnv(const wxString
& var
, wxString
*value
);
229 // set the env var name to the given value, return TRUE on success
230 WXDLLEXPORT
bool wxSetEnv(const wxString
& var
, const wxChar
*value
);
232 // remove the env var from environment
233 inline bool wxUnsetEnv(const wxString
& var
) { return wxSetEnv(var
, NULL
); }
235 // ----------------------------------------------------------------------------
236 // Network and username functions.
237 // ----------------------------------------------------------------------------
239 // NB: "char *" functions are deprecated, use wxString ones!
242 WXDLLEXPORT
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
243 WXDLLEXPORT wxString
wxGetEmailAddress();
246 WXDLLEXPORT
bool wxGetHostName(wxChar
*buf
, int maxSize
);
247 WXDLLEXPORT wxString
wxGetHostName();
250 WXDLLEXPORT wxString
wxGetFullHostName();
251 WXDLLEXPORT
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
253 // Get user ID e.g. jacs (this is known as login name under Unix)
254 WXDLLEXPORT
bool wxGetUserId(wxChar
*buf
, int maxSize
);
255 WXDLLEXPORT wxString
wxGetUserId();
257 // Get user name e.g. Julian Smart
258 WXDLLEXPORT
bool wxGetUserName(wxChar
*buf
, int maxSize
);
259 WXDLLEXPORT wxString
wxGetUserName();
261 // Get current Home dir and copy to dest (returns pstr->c_str())
262 WXDLLEXPORT wxString
wxGetHomeDir();
263 WXDLLEXPORT
const wxChar
* wxGetHomeDir(wxString
*pstr
);
265 // Get the user's home dir (caller must copy --- volatile)
266 // returns NULL is no HOME dir is known
267 #if defined(__UNIX__) && wxUSE_UNICODE
268 WXDLLEXPORT
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
270 WXDLLEXPORT wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
274 WXDLLEXPORT wxString
wxMacFindFolder(short vRefNum
,
276 Boolean createFolder
);
279 // get number of total/free bytes on the disk where path belongs
280 WXDLLEXPORT
bool wxGetDiskSpace(const wxString
& path
,
281 wxLongLong
*pTotal
= NULL
,
282 wxLongLong
*pFree
= NULL
);
284 #if wxUSE_GUI // GUI only things from now on
286 // ----------------------------------------------------------------------------
287 // Menu accelerators related things
288 // ----------------------------------------------------------------------------
290 WXDLLEXPORT wxChar
* wxStripMenuCodes(wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
291 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
294 class WXDLLEXPORT wxAcceleratorEntry
;
295 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
296 #endif // wxUSE_ACCEL
298 // ----------------------------------------------------------------------------
300 // ----------------------------------------------------------------------------
302 // Find the window/widget with the given title or label.
303 // Pass a parent to begin the search from, or NULL to look through
305 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
307 // Find window by name, and if that fails, by label.
308 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
310 // Returns menu item id or -1 if none.
311 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
313 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
314 // is always present but may be less reliable than a native version.
315 WXDLLEXPORT wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
);
316 WXDLLEXPORT wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
);
318 // ----------------------------------------------------------------------------
319 // Message/event queue helpers
320 // ----------------------------------------------------------------------------
322 // Yield to other apps/messages
323 WXDLLEXPORT
bool wxYield();
325 // Like wxYield, but fails silently if the yield is recursive.
326 WXDLLEXPORT
bool wxYieldIfNeeded();
328 // Yield to other apps/messages and disable user input
329 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
);
331 // Enable or disable input to all top level windows
332 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= TRUE
);
334 // Check whether this window wants to process messages, e.g. Stop button
335 // in long calculations.
336 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
338 // Consume all events until no more left
339 WXDLLEXPORT
void wxFlushEvents();
341 // a class which disables all windows (except, may be, thegiven one) in its
342 // ctor and enables them back in its dtor
343 class WXDLLEXPORT wxWindowDisabler
346 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
350 wxWindowList
*m_winDisabled
;
353 // ----------------------------------------------------------------------------
355 // ----------------------------------------------------------------------------
357 // Set the cursor to the busy cursor for all windows
358 class WXDLLEXPORT wxCursor
;
359 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
360 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
362 // Restore cursor to normal
363 WXDLLEXPORT
void wxEndBusyCursor();
365 // TRUE if we're between the above two calls
366 WXDLLEXPORT
bool wxIsBusy();
368 // Convenience class so we can just create a wxBusyCursor object on the stack
369 class WXDLLEXPORT wxBusyCursor
372 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
373 { wxBeginBusyCursor(cursor
); }
375 { wxEndBusyCursor(); }
377 // FIXME: These two methods are currently only implemented (and needed?)
378 // in wxGTK. BusyCursor handling should probably be moved to
379 // common code since the wxGTK and wxMSW implementations are very
380 // similar except for wxMSW using HCURSOR directly instead of
382 static const wxCursor
&GetStoredCursor();
383 static const wxCursor
GetBusyCursor();
387 // ----------------------------------------------------------------------------
388 // Reading and writing resources (eg WIN.INI, .Xdefaults)
389 // ----------------------------------------------------------------------------
392 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
393 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
394 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
395 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
397 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
398 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
399 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
400 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
401 #endif // wxUSE_RESOURCES
403 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
405 // MSW only: get user-defined resource from the .res file.
406 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
408 WXDLLEXPORT
extern const wxChar
* wxUserResourceStr
;
409 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
411 // Implemented in utils.cpp: VC++, Win95 only. Sets up a console for standard
413 WXDLLEXPORT
void wxRedirectIOToConsole();
417 // ----------------------------------------------------------------------------
418 // Display and colorss (X only)
419 // ----------------------------------------------------------------------------
422 void *wxGetDisplay();
426 WXDisplay
*wxGetDisplay();
427 bool wxSetDisplay(const wxString
& display_name
);
428 wxString
wxGetDisplayName();
433 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
434 // The resulting warnings are switched off here
435 #pragma message disable nosimpint
437 #include <X11/Xlib.h>
439 #pragma message enable nosimpint
442 #define wxMAX_RGB 0xff
443 #define wxMAX_SV 1000
444 #define wxSIGN(x) ((x < 0) ? -x : x)
449 typedef struct wx_hsv
{
453 #define wxMax3(x,y,z) ((x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z))
454 #define wxMin3(x,y,z) ((x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z))
456 void wxHSVToXColor(wxHSV
*hsv
,XColor
*xcolor
);
457 void wxXColorToHSV(wxHSV
*hsv
,XColor
*xcolor
);
458 void wxAllocNearestColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
459 void wxAllocColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
465 // ----------------------------------------------------------------------------
466 // Error message functions used by wxWindows (deprecated, use wxLog)
467 // ----------------------------------------------------------------------------
469 // Format a message on the standard error (UNIX) or the debugging
471 WXDLLEXPORT
void wxDebugMsg(const wxChar
*fmt
...);
473 // Non-fatal error (continues)
474 WXDLLEXPORT_DATA(extern const wxChar
*) wxInternalErrorStr
;
475 WXDLLEXPORT
void wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
477 // Fatal error (exits)
478 WXDLLEXPORT_DATA(extern const wxChar
*) wxFatalErrorStr
;
479 WXDLLEXPORT
void wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);