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"
35 // ----------------------------------------------------------------------------
36 // Forward declaration
37 // ----------------------------------------------------------------------------
39 class WXDLLEXPORT wxProcess
;
40 class WXDLLEXPORT wxFrame
;
41 class WXDLLEXPORT wxWindow
;
42 class WXDLLEXPORT wxWindowList
;
43 class WXDLLEXPORT wxPoint
;
45 // FIXME should use wxStricmp() instead
46 #if defined(__GNUWIN32__)
47 #define stricmp strcasecmp
48 #define strnicmp strncasecmp
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
56 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
58 // ----------------------------------------------------------------------------
59 // String functions (deprecated, use wxString)
60 // ----------------------------------------------------------------------------
62 // Useful buffer (FIXME VZ: To be removed!!!)
63 WXDLLEXPORT_DATA(extern wxChar
*) wxBuffer
;
65 // Make a copy of this string using 'new'
66 WXDLLEXPORT wxChar
* copystring(const wxChar
*s
);
68 // Matches string one within string two regardless of case
69 WXDLLEXPORT
bool StringMatch(wxChar
*one
, wxChar
*two
, bool subString
= TRUE
, bool exact
= FALSE
);
71 // A shorter way of using strcmp
72 #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
74 // ----------------------------------------------------------------------------
75 // Miscellaneous functions
76 // ----------------------------------------------------------------------------
79 WXDLLEXPORT
void wxBell();
81 // Get OS description as a user-readable string
82 WXDLLEXPORT wxString
wxGetOsDescription();
85 WXDLLEXPORT
int wxGetOsVersion(int *majorVsn
= (int *) NULL
,
86 int *minorVsn
= (int *) NULL
);
88 // Return a string with the current date/time
89 WXDLLEXPORT wxString
wxNow();
92 // Don't synthesize KeyUp events holding down a key and producing
93 // KeyDown events with autorepeat. On by default and always on
95 WXDLLEXPORT
bool wxSetDetectableAutoRepeat( bool flag
);
97 // ----------------------------------------------------------------------------
98 // Window ID management
99 // ----------------------------------------------------------------------------
101 // Generate a unique ID
102 WXDLLEXPORT
long wxNewId();
103 #if !defined(NewId) && defined(WXWIN_COMPATIBILITY)
104 #define NewId wxNewId
107 // Ensure subsequent IDs don't clash with this one
108 WXDLLEXPORT
void wxRegisterId(long id
);
109 #if !defined(RegisterId) && defined(WXWIN_COMPATIBILITY)
110 #define RegisterId wxRegisterId
113 // Return the current ID
114 WXDLLEXPORT
long wxGetCurrentId();
118 // ----------------------------------------------------------------------------
119 // Various conversions
120 // ----------------------------------------------------------------------------
122 WXDLLEXPORT_DATA(extern const wxChar
*) wxFloatToStringStr
;
123 WXDLLEXPORT_DATA(extern const wxChar
*) wxDoubleToStringStr
;
125 WXDLLEXPORT
void StringToFloat(wxChar
*s
, float *number
);
126 WXDLLEXPORT wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
);
127 WXDLLEXPORT
void StringToDouble(wxChar
*s
, double *number
);
128 WXDLLEXPORT wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
);
129 WXDLLEXPORT
void StringToInt(wxChar
*s
, int *number
);
130 WXDLLEXPORT
void StringToLong(wxChar
*s
, long *number
);
131 WXDLLEXPORT wxChar
* IntToString(int number
);
132 WXDLLEXPORT wxChar
* LongToString(long number
);
134 // Convert 2-digit hex number to decimal
135 WXDLLEXPORT
int wxHexToDec(const wxString
& buf
);
137 // Convert decimal integer to 2-character hex string
138 WXDLLEXPORT
void wxDecToHex(int dec
, wxChar
*buf
);
139 WXDLLEXPORT wxString
wxDecToHex(int dec
);
141 // ----------------------------------------------------------------------------
142 // Process management
143 // ----------------------------------------------------------------------------
145 // Execute another program. Returns 0 if there was an error, a PID otherwise.
146 WXDLLEXPORT
long wxExecute(wxChar
**argv
, bool sync
= FALSE
,
147 wxProcess
*process
= (wxProcess
*) NULL
);
148 WXDLLEXPORT
long wxExecute(const wxString
& command
, bool sync
= FALSE
,
149 wxProcess
*process
= (wxProcess
*) NULL
);
151 // execute the command capturing its output into an array line by line
152 WXDLLEXPORT
long wxExecute(const wxString
& command
,
153 wxArrayString
& output
);
155 // also capture stderr
156 WXDLLEXPORT
long wxExecute(const wxString
& command
,
157 wxArrayString
& output
,
158 wxArrayString
& error
);
162 wxSIGNONE
= 0, // verify if the process exists under Unix
169 wxSIGIOT
= wxSIGABRT
, // another name
180 // further signals are different in meaning between different Unix systems
185 wxKILL_OK
, // no error
186 wxKILL_BAD_SIGNAL
, // no such signal
187 wxKILL_ACCESS_DENIED
, // permission denied
188 wxKILL_NO_PROCESS
, // no such process
189 wxKILL_ERROR
// another, unspecified error
192 // send the given signal to the process (only NONE and KILL are supported under
193 // Windows, all others mean TERM), return 0 if ok and -1 on error
195 // return detailed error in rc if not NULL
196 WXDLLEXPORT
int wxKill(long pid
,
197 wxSignal sig
= wxSIGTERM
,
198 wxKillError
*rc
= NULL
);
200 // Execute a command in an interactive shell window (always synchronously)
201 // If no command then just the shell
202 WXDLLEXPORT
bool wxShell(const wxString
& command
= wxEmptyString
);
204 // As wxShell(), but must give a (non interactive) command and its output will
205 // be returned in output array
206 WXDLLEXPORT
bool wxShell(const wxString
& command
, wxArrayString
& output
);
208 // Sleep for nSecs seconds
209 WXDLLEXPORT
void wxSleep(int nSecs
);
211 // Sleep for a given amount of milliseconds
212 WXDLLEXPORT
void wxUsleep(unsigned long milliseconds
);
214 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
215 WXDLLEXPORT
long wxGetFreeMemory();
217 // should wxApp::OnFatalException() be called?
218 WXDLLEXPORT
bool wxHandleFatalExceptions(bool doit
= TRUE
);
220 // ----------------------------------------------------------------------------
221 // Environment variables
222 // ----------------------------------------------------------------------------
224 // returns TRUE if variable exists (value may be NULL if you just want to check
226 WXDLLEXPORT
bool wxGetEnv(const wxString
& var
, wxString
*value
);
228 // set the env var name to the given value, return TRUE on success
229 WXDLLEXPORT
bool wxSetEnv(const wxString
& var
, const wxChar
*value
);
231 // remove the env var from environment
232 inline bool wxUnsetEnv(const wxString
& var
) { return wxSetEnv(var
, NULL
); }
234 // ----------------------------------------------------------------------------
235 // Network and username functions.
236 // ----------------------------------------------------------------------------
238 // NB: "char *" functions are deprecated, use wxString ones!
241 WXDLLEXPORT
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
242 WXDLLEXPORT wxString
wxGetEmailAddress();
245 WXDLLEXPORT
bool wxGetHostName(wxChar
*buf
, int maxSize
);
246 WXDLLEXPORT wxString
wxGetHostName();
249 WXDLLEXPORT wxString
wxGetFullHostName();
250 WXDLLEXPORT
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
252 // Get user ID e.g. jacs (this is known as login name under Unix)
253 WXDLLEXPORT
bool wxGetUserId(wxChar
*buf
, int maxSize
);
254 WXDLLEXPORT wxString
wxGetUserId();
256 // Get user name e.g. Julian Smart
257 WXDLLEXPORT
bool wxGetUserName(wxChar
*buf
, int maxSize
);
258 WXDLLEXPORT wxString
wxGetUserName();
260 // Get current Home dir and copy to dest (returns pstr->c_str())
261 WXDLLEXPORT wxString
wxGetHomeDir();
262 WXDLLEXPORT
const wxChar
* wxGetHomeDir(wxString
*pstr
);
264 // Get the user's home dir (caller must copy --- volatile)
265 // returns NULL is no HOME dir is known
266 #if defined(__UNIX__) && wxUSE_UNICODE
267 WXDLLEXPORT
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
269 WXDLLEXPORT wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
273 WXDLLEXPORT wxString
wxMacFindFolder(short vRefNum
,
275 Boolean createFolder
);
278 #if wxUSE_GUI // GUI only things from now on
280 // ----------------------------------------------------------------------------
281 // Menu accelerators related things
282 // ----------------------------------------------------------------------------
284 WXDLLEXPORT wxChar
* wxStripMenuCodes(wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
285 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
288 class WXDLLEXPORT wxAcceleratorEntry
;
289 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
290 #endif // wxUSE_ACCEL
292 // ----------------------------------------------------------------------------
294 // ----------------------------------------------------------------------------
296 // Find the window/widget with the given title or label.
297 // Pass a parent to begin the search from, or NULL to look through
299 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
301 // Find window by name, and if that fails, by label.
302 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
304 // Returns menu item id or -1 if none.
305 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
307 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
308 // is always present but may be less reliable than a native version.
309 WXDLLEXPORT wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
);
310 WXDLLEXPORT wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
);
312 // ----------------------------------------------------------------------------
313 // Message/event queue helpers
314 // ----------------------------------------------------------------------------
316 // Yield to other apps/messages
317 WXDLLEXPORT
bool wxYield();
319 // Like wxYield, but fails silently if the yield is recursive.
320 WXDLLEXPORT
bool wxYieldIfNeeded();
322 // Yield to other apps/messages and disable user input
323 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
);
325 // Enable or disable input to all top level windows
326 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= TRUE
);
328 // Check whether this window wants to process messages, e.g. Stop button
329 // in long calculations.
330 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
332 // Consume all events until no more left
333 WXDLLEXPORT
void wxFlushEvents();
335 // a class which disables all windows (except, may be, thegiven one) in its
336 // ctor and enables them back in its dtor
337 class WXDLLEXPORT wxWindowDisabler
340 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
344 wxWindowList
*m_winDisabled
;
347 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
351 // Set the cursor to the busy cursor for all windows
352 class WXDLLEXPORT wxCursor
;
353 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
354 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
356 // Restore cursor to normal
357 WXDLLEXPORT
void wxEndBusyCursor();
359 // TRUE if we're between the above two calls
360 WXDLLEXPORT
bool wxIsBusy();
362 // Convenience class so we can just create a wxBusyCursor object on the stack
363 class WXDLLEXPORT wxBusyCursor
366 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
367 { wxBeginBusyCursor(cursor
); }
369 { wxEndBusyCursor(); }
371 // FIXME: These two methods are currently only implemented (and needed?)
372 // in wxGTK. BusyCursor handling should probably be moved to
373 // common code since the wxGTK and wxMSW implementations are very
374 // similar except for wxMSW using HCURSOR directly instead of
376 static const wxCursor
&GetStoredCursor();
377 static const wxCursor
GetBusyCursor();
381 // ----------------------------------------------------------------------------
382 // Reading and writing resources (eg WIN.INI, .Xdefaults)
383 // ----------------------------------------------------------------------------
386 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
387 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
388 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
389 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
391 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
392 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
393 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
394 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
395 #endif // wxUSE_RESOURCES
397 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
399 // MSW only: get user-defined resource from the .res file.
400 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
402 WXDLLEXPORT
extern const wxChar
* wxUserResourceStr
;
403 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
405 // Implemented in utils.cpp: VC++, Win95 only. Sets up a console for standard
407 WXDLLEXPORT
void wxRedirectIOToConsole();
411 // ----------------------------------------------------------------------------
412 // Display and colorss (X only)
413 // ----------------------------------------------------------------------------
416 void *wxGetDisplay();
420 WXDisplay
*wxGetDisplay();
421 bool wxSetDisplay(const wxString
& display_name
);
422 wxString
wxGetDisplayName();
427 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
428 // The resulting warnings are switched off here
429 #pragma message disable nosimpint
431 #include <X11/Xlib.h>
433 #pragma message enable nosimpint
436 #define wxMAX_RGB 0xff
437 #define wxMAX_SV 1000
438 #define wxSIGN(x) ((x < 0) ? -x : x)
443 typedef struct wx_hsv
{
447 #define wxMax3(x,y,z) ((x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z))
448 #define wxMin3(x,y,z) ((x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z))
450 void wxHSVToXColor(wxHSV
*hsv
,XColor
*xcolor
);
451 void wxXColorToHSV(wxHSV
*hsv
,XColor
*xcolor
);
452 void wxAllocNearestColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
453 void wxAllocColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
459 // ----------------------------------------------------------------------------
460 // Error message functions used by wxWindows (deprecated, use wxLog)
461 // ----------------------------------------------------------------------------
463 // Format a message on the standard error (UNIX) or the debugging
465 WXDLLEXPORT
void wxDebugMsg(const wxChar
*fmt
...);
467 // Non-fatal error (continues)
468 WXDLLEXPORT_DATA(extern const wxChar
*) wxInternalErrorStr
;
469 WXDLLEXPORT
void wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
471 // Fatal error (exits)
472 WXDLLEXPORT_DATA(extern const wxChar
*) wxFatalErrorStr
;
473 WXDLLEXPORT
void wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);