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();
89 // Return path where wxWindows is installed (mostly useful in Unices)
90 WXDLLEXPORT
const wxChar
*wxGetInstallPrefix();
94 // Don't synthesize KeyUp events holding down a key and producing
95 // KeyDown events with autorepeat. On by default and always on
97 WXDLLEXPORT
bool wxSetDetectableAutoRepeat( bool flag
);
99 // ----------------------------------------------------------------------------
100 // Window ID management
101 // ----------------------------------------------------------------------------
103 // Generate a unique ID
104 WXDLLEXPORT
long wxNewId();
105 #if !defined(NewId) && defined(WXWIN_COMPATIBILITY)
106 #define NewId wxNewId
109 // Ensure subsequent IDs don't clash with this one
110 WXDLLEXPORT
void wxRegisterId(long id
);
111 #if !defined(RegisterId) && defined(WXWIN_COMPATIBILITY)
112 #define RegisterId wxRegisterId
115 // Return the current ID
116 WXDLLEXPORT
long wxGetCurrentId();
120 // ----------------------------------------------------------------------------
121 // Various conversions
122 // ----------------------------------------------------------------------------
124 WXDLLEXPORT_DATA(extern const wxChar
*) wxFloatToStringStr
;
125 WXDLLEXPORT_DATA(extern const wxChar
*) wxDoubleToStringStr
;
127 WXDLLEXPORT
void StringToFloat(wxChar
*s
, float *number
);
128 WXDLLEXPORT wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
);
129 WXDLLEXPORT
void StringToDouble(wxChar
*s
, double *number
);
130 WXDLLEXPORT wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
);
131 WXDLLEXPORT
void StringToInt(wxChar
*s
, int *number
);
132 WXDLLEXPORT
void StringToLong(wxChar
*s
, long *number
);
133 WXDLLEXPORT wxChar
* IntToString(int number
);
134 WXDLLEXPORT wxChar
* LongToString(long number
);
136 // Convert 2-digit hex number to decimal
137 WXDLLEXPORT
int wxHexToDec(const wxString
& buf
);
139 // Convert decimal integer to 2-character hex string
140 WXDLLEXPORT
void wxDecToHex(int dec
, wxChar
*buf
);
141 WXDLLEXPORT wxString
wxDecToHex(int dec
);
143 // ----------------------------------------------------------------------------
144 // Process management
145 // ----------------------------------------------------------------------------
147 // Execute another program. Returns 0 if there was an error, a PID otherwise.
148 WXDLLEXPORT
long wxExecute(wxChar
**argv
, bool sync
= FALSE
,
149 wxProcess
*process
= (wxProcess
*) NULL
);
150 WXDLLEXPORT
long wxExecute(const wxString
& command
, bool sync
= FALSE
,
151 wxProcess
*process
= (wxProcess
*) NULL
);
153 // execute the command capturing its output into an array line by line
154 WXDLLEXPORT
long wxExecute(const wxString
& command
,
155 wxArrayString
& output
);
157 // also capture stderr
158 WXDLLEXPORT
long wxExecute(const wxString
& command
,
159 wxArrayString
& output
,
160 wxArrayString
& error
);
164 wxSIGNONE
= 0, // verify if the process exists under Unix
171 wxSIGIOT
= wxSIGABRT
, // another name
182 // further signals are different in meaning between different Unix systems
187 wxKILL_OK
, // no error
188 wxKILL_BAD_SIGNAL
, // no such signal
189 wxKILL_ACCESS_DENIED
, // permission denied
190 wxKILL_NO_PROCESS
, // no such process
191 wxKILL_ERROR
// another, unspecified error
194 // send the given signal to the process (only NONE and KILL are supported under
195 // Windows, all others mean TERM), return 0 if ok and -1 on error
197 // return detailed error in rc if not NULL
198 WXDLLEXPORT
int wxKill(long pid
,
199 wxSignal sig
= wxSIGTERM
,
200 wxKillError
*rc
= NULL
);
202 // Execute a command in an interactive shell window (always synchronously)
203 // If no command then just the shell
204 WXDLLEXPORT
bool wxShell(const wxString
& command
= wxEmptyString
);
206 // As wxShell(), but must give a (non interactive) command and its output will
207 // be returned in output array
208 WXDLLEXPORT
bool wxShell(const wxString
& command
, wxArrayString
& output
);
210 // Sleep for nSecs seconds
211 WXDLLEXPORT
void wxSleep(int nSecs
);
213 // Sleep for a given amount of milliseconds
214 WXDLLEXPORT
void wxUsleep(unsigned long milliseconds
);
216 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
217 WXDLLEXPORT
long wxGetFreeMemory();
219 // should wxApp::OnFatalException() be called?
220 WXDLLEXPORT
bool wxHandleFatalExceptions(bool doit
= TRUE
);
222 // ----------------------------------------------------------------------------
223 // Environment variables
224 // ----------------------------------------------------------------------------
226 // returns TRUE if variable exists (value may be NULL if you just want to check
228 WXDLLEXPORT
bool wxGetEnv(const wxString
& var
, wxString
*value
);
230 // set the env var name to the given value, return TRUE on success
231 WXDLLEXPORT
bool wxSetEnv(const wxString
& var
, const wxChar
*value
);
233 // remove the env var from environment
234 inline bool wxUnsetEnv(const wxString
& var
) { return wxSetEnv(var
, NULL
); }
236 // ----------------------------------------------------------------------------
237 // Network and username functions.
238 // ----------------------------------------------------------------------------
240 // NB: "char *" functions are deprecated, use wxString ones!
243 WXDLLEXPORT
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
244 WXDLLEXPORT wxString
wxGetEmailAddress();
247 WXDLLEXPORT
bool wxGetHostName(wxChar
*buf
, int maxSize
);
248 WXDLLEXPORT wxString
wxGetHostName();
251 WXDLLEXPORT wxString
wxGetFullHostName();
252 WXDLLEXPORT
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
254 // Get user ID e.g. jacs (this is known as login name under Unix)
255 WXDLLEXPORT
bool wxGetUserId(wxChar
*buf
, int maxSize
);
256 WXDLLEXPORT wxString
wxGetUserId();
258 // Get user name e.g. Julian Smart
259 WXDLLEXPORT
bool wxGetUserName(wxChar
*buf
, int maxSize
);
260 WXDLLEXPORT wxString
wxGetUserName();
262 // Get current Home dir and copy to dest (returns pstr->c_str())
263 WXDLLEXPORT wxString
wxGetHomeDir();
264 WXDLLEXPORT
const wxChar
* wxGetHomeDir(wxString
*pstr
);
266 // Get the user's home dir (caller must copy --- volatile)
267 // returns NULL is no HOME dir is known
268 #if defined(__UNIX__) && wxUSE_UNICODE
269 WXDLLEXPORT
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
271 WXDLLEXPORT wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
275 WXDLLEXPORT wxString
wxMacFindFolder(short vRefNum
,
277 Boolean createFolder
);
280 // get number of total/free bytes on the disk where path belongs
281 WXDLLEXPORT
bool wxGetDiskSpace(const wxString
& path
,
282 wxLongLong
*pTotal
= NULL
,
283 wxLongLong
*pFree
= NULL
);
285 #if wxUSE_GUI // GUI only things from now on
287 // ----------------------------------------------------------------------------
288 // Menu accelerators related things
289 // ----------------------------------------------------------------------------
291 WXDLLEXPORT wxChar
* wxStripMenuCodes(wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
292 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
295 class WXDLLEXPORT wxAcceleratorEntry
;
296 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
297 #endif // wxUSE_ACCEL
299 // ----------------------------------------------------------------------------
301 // ----------------------------------------------------------------------------
303 // Find the window/widget with the given title or label.
304 // Pass a parent to begin the search from, or NULL to look through
306 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
308 // Find window by name, and if that fails, by label.
309 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
311 // Returns menu item id or -1 if none.
312 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
314 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
315 // is always present but may be less reliable than a native version.
316 WXDLLEXPORT wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
);
317 WXDLLEXPORT wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
);
319 // ----------------------------------------------------------------------------
320 // Message/event queue helpers
321 // ----------------------------------------------------------------------------
323 // Yield to other apps/messages
324 WXDLLEXPORT
bool wxYield();
326 // Like wxYield, but fails silently if the yield is recursive.
327 WXDLLEXPORT
bool wxYieldIfNeeded();
329 // Yield to other apps/messages and disable user input
330 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
);
332 // Enable or disable input to all top level windows
333 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= TRUE
);
335 // Check whether this window wants to process messages, e.g. Stop button
336 // in long calculations.
337 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
339 // Consume all events until no more left
340 WXDLLEXPORT
void wxFlushEvents();
342 // a class which disables all windows (except, may be, thegiven one) in its
343 // ctor and enables them back in its dtor
344 class WXDLLEXPORT wxWindowDisabler
347 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
351 wxWindowList
*m_winDisabled
;
354 // ----------------------------------------------------------------------------
356 // ----------------------------------------------------------------------------
358 // Set the cursor to the busy cursor for all windows
359 class WXDLLEXPORT wxCursor
;
360 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
361 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
363 // Restore cursor to normal
364 WXDLLEXPORT
void wxEndBusyCursor();
366 // TRUE if we're between the above two calls
367 WXDLLEXPORT
bool wxIsBusy();
369 // Convenience class so we can just create a wxBusyCursor object on the stack
370 class WXDLLEXPORT wxBusyCursor
373 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
374 { wxBeginBusyCursor(cursor
); }
376 { wxEndBusyCursor(); }
378 // FIXME: These two methods are currently only implemented (and needed?)
379 // in wxGTK. BusyCursor handling should probably be moved to
380 // common code since the wxGTK and wxMSW implementations are very
381 // similar except for wxMSW using HCURSOR directly instead of
383 static const wxCursor
&GetStoredCursor();
384 static const wxCursor
GetBusyCursor();
388 // ----------------------------------------------------------------------------
389 // Reading and writing resources (eg WIN.INI, .Xdefaults)
390 // ----------------------------------------------------------------------------
393 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
394 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
395 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
396 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
398 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
399 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
400 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
401 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
402 #endif // wxUSE_RESOURCES
404 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
406 // MSW only: get user-defined resource from the .res file.
407 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
409 WXDLLEXPORT
extern const wxChar
* wxUserResourceStr
;
410 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
412 // Implemented in utils.cpp: VC++, Win95 only. Sets up a console for standard
414 WXDLLEXPORT
void wxRedirectIOToConsole();
418 // ----------------------------------------------------------------------------
419 // Display and colorss (X only)
420 // ----------------------------------------------------------------------------
423 void *wxGetDisplay();
427 WXDisplay
*wxGetDisplay();
428 bool wxSetDisplay(const wxString
& display_name
);
429 wxString
wxGetDisplayName();
434 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
435 // The resulting warnings are switched off here
436 #pragma message disable nosimpint
438 #include <X11/Xlib.h>
440 #pragma message enable nosimpint
443 #define wxMAX_RGB 0xff
444 #define wxMAX_SV 1000
445 #define wxSIGN(x) ((x < 0) ? -x : x)
450 typedef struct wx_hsv
{
454 #define wxMax3(x,y,z) ((x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z))
455 #define wxMin3(x,y,z) ((x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z))
457 void wxHSVToXColor(wxHSV
*hsv
,XColor
*xcolor
);
458 void wxXColorToHSV(wxHSV
*hsv
,XColor
*xcolor
);
459 void wxAllocNearestColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
460 void wxAllocColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
466 // ----------------------------------------------------------------------------
467 // Error message functions used by wxWindows (deprecated, use wxLog)
468 // ----------------------------------------------------------------------------
470 // Format a message on the standard error (UNIX) or the debugging
472 WXDLLEXPORT
void wxDebugMsg(const wxChar
*fmt
...);
474 // Non-fatal error (continues)
475 WXDLLEXPORT_DATA(extern const wxChar
*) wxInternalErrorStr
;
476 WXDLLEXPORT
void wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
478 // Fatal error (exits)
479 WXDLLEXPORT_DATA(extern const wxChar
*) wxFatalErrorStr
;
480 WXDLLEXPORT
void wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);