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 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
50 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
52 // ----------------------------------------------------------------------------
53 // String functions (deprecated, use wxString)
54 // ----------------------------------------------------------------------------
56 // Useful buffer (FIXME VZ: To be removed!!!)
57 WXDLLEXPORT_DATA(extern wxChar
*) wxBuffer
;
59 // Make a copy of this string using 'new'
60 WXDLLEXPORT wxChar
* copystring(const wxChar
*s
);
62 // Matches string one within string two regardless of case
63 WXDLLEXPORT
bool StringMatch(wxChar
*one
, wxChar
*two
, bool subString
= TRUE
, bool exact
= FALSE
);
65 // A shorter way of using strcmp
66 #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
68 // ----------------------------------------------------------------------------
69 // Miscellaneous functions
70 // ----------------------------------------------------------------------------
73 WXDLLEXPORT
void wxBell();
75 // Get OS description as a user-readable string
76 WXDLLEXPORT wxString
wxGetOsDescription();
79 WXDLLEXPORT
int wxGetOsVersion(int *majorVsn
= (int *) NULL
,
80 int *minorVsn
= (int *) NULL
);
82 // Return a string with the current date/time
83 WXDLLEXPORT wxString
wxNow();
86 // Don't synthesize KeyUp events holding down a key and producing
87 // KeyDown events with autorepeat. On by default and always on
89 WXDLLEXPORT
bool wxSetDetectableAutoRepeat( bool flag
);
91 // ----------------------------------------------------------------------------
92 // Window ID management
93 // ----------------------------------------------------------------------------
95 // Generate a unique ID
96 WXDLLEXPORT
long wxNewId();
97 #if !defined(NewId) && defined(WXWIN_COMPATIBILITY)
101 // Ensure subsequent IDs don't clash with this one
102 WXDLLEXPORT
void wxRegisterId(long id
);
103 #if !defined(RegisterId) && defined(WXWIN_COMPATIBILITY)
104 #define RegisterId wxRegisterId
107 // Return the current ID
108 WXDLLEXPORT
long wxGetCurrentId();
112 // ----------------------------------------------------------------------------
113 // Various conversions
114 // ----------------------------------------------------------------------------
116 WXDLLEXPORT_DATA(extern const wxChar
*) wxFloatToStringStr
;
117 WXDLLEXPORT_DATA(extern const wxChar
*) wxDoubleToStringStr
;
119 WXDLLEXPORT
void StringToFloat(wxChar
*s
, float *number
);
120 WXDLLEXPORT wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
);
121 WXDLLEXPORT
void StringToDouble(wxChar
*s
, double *number
);
122 WXDLLEXPORT wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
);
123 WXDLLEXPORT
void StringToInt(wxChar
*s
, int *number
);
124 WXDLLEXPORT
void StringToLong(wxChar
*s
, long *number
);
125 WXDLLEXPORT wxChar
* IntToString(int number
);
126 WXDLLEXPORT wxChar
* LongToString(long number
);
128 // Convert 2-digit hex number to decimal
129 WXDLLEXPORT
int wxHexToDec(const wxString
& buf
);
131 // Convert decimal integer to 2-character hex string
132 WXDLLEXPORT
void wxDecToHex(int dec
, wxChar
*buf
);
133 WXDLLEXPORT wxString
wxDecToHex(int dec
);
135 // ----------------------------------------------------------------------------
136 // Process management
137 // ----------------------------------------------------------------------------
139 // Execute another program. Returns 0 if there was an error, a PID otherwise.
140 WXDLLEXPORT
long wxExecute(wxChar
**argv
, bool sync
= FALSE
,
141 wxProcess
*process
= (wxProcess
*) NULL
);
142 WXDLLEXPORT
long wxExecute(const wxString
& command
, bool sync
= FALSE
,
143 wxProcess
*process
= (wxProcess
*) NULL
);
145 // execute the command capturing its output into an array line by line
146 WXDLLEXPORT
long wxExecute(const wxString
& command
,
147 wxArrayString
& output
);
149 // also capture stderr
150 WXDLLEXPORT
long wxExecute(const wxString
& command
,
151 wxArrayString
& output
,
152 wxArrayString
& error
);
156 wxSIGNONE
= 0, // verify if the process exists under Unix
163 wxSIGIOT
= wxSIGABRT
, // another name
174 // further signals are different in meaning between different Unix systems
179 wxKILL_OK
, // no error
180 wxKILL_BAD_SIGNAL
, // no such signal
181 wxKILL_ACCESS_DENIED
, // permission denied
182 wxKILL_NO_PROCESS
, // no such process
183 wxKILL_ERROR
// another, unspecified error
186 // send the given signal to the process (only NONE and KILL are supported under
187 // Windows, all others mean TERM), return 0 if ok and -1 on error
189 // return detailed error in rc if not NULL
190 WXDLLEXPORT
int wxKill(long pid
,
191 wxSignal sig
= wxSIGTERM
,
192 wxKillError
*rc
= NULL
);
194 // Execute a command in an interactive shell window (always synchronously)
195 // If no command then just the shell
196 WXDLLEXPORT
bool wxShell(const wxString
& command
= wxEmptyString
);
198 // As wxShell(), but must give a (non interactive) command and its output will
199 // be returned in output array
200 WXDLLEXPORT
bool wxShell(const wxString
& command
, wxArrayString
& output
);
202 // Sleep for nSecs seconds
203 WXDLLEXPORT
void wxSleep(int nSecs
);
205 // Sleep for a given amount of milliseconds
206 WXDLLEXPORT
void wxUsleep(unsigned long milliseconds
);
208 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
209 WXDLLEXPORT
long wxGetFreeMemory();
211 // should wxApp::OnFatalException() be called?
212 WXDLLEXPORT
bool wxHandleFatalExceptions(bool doit
= TRUE
);
214 // ----------------------------------------------------------------------------
215 // Environment variables
216 // ----------------------------------------------------------------------------
218 // returns TRUE if variable exists (value may be NULL if you just want to check
220 WXDLLEXPORT
bool wxGetEnv(const wxString
& var
, wxString
*value
);
222 // set the env var name to the given value, return TRUE on success
223 WXDLLEXPORT
bool wxSetEnv(const wxString
& var
, const wxChar
*value
);
225 // remove the env var from environment
226 inline bool wxUnsetEnv(const wxString
& var
) { return wxSetEnv(var
, NULL
); }
228 // ----------------------------------------------------------------------------
229 // Network and username functions.
230 // ----------------------------------------------------------------------------
232 // NB: "char *" functions are deprecated, use wxString ones!
235 WXDLLEXPORT
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
236 WXDLLEXPORT wxString
wxGetEmailAddress();
239 WXDLLEXPORT
bool wxGetHostName(wxChar
*buf
, int maxSize
);
240 WXDLLEXPORT wxString
wxGetHostName();
243 WXDLLEXPORT wxString
wxGetFullHostName();
244 WXDLLEXPORT
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
246 // Get user ID e.g. jacs (this is known as login name under Unix)
247 WXDLLEXPORT
bool wxGetUserId(wxChar
*buf
, int maxSize
);
248 WXDLLEXPORT wxString
wxGetUserId();
250 // Get user name e.g. Julian Smart
251 WXDLLEXPORT
bool wxGetUserName(wxChar
*buf
, int maxSize
);
252 WXDLLEXPORT wxString
wxGetUserName();
254 // Get current Home dir and copy to dest (returns pstr->c_str())
255 WXDLLEXPORT wxString
wxGetHomeDir();
256 WXDLLEXPORT
const wxChar
* wxGetHomeDir(wxString
*pstr
);
258 // Get the user's home dir (caller must copy --- volatile)
259 // returns NULL is no HOME dir is known
260 #if defined(__UNIX__) && wxUSE_UNICODE
261 WXDLLEXPORT
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
263 WXDLLEXPORT wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
267 WXDLLEXPORT wxString
wxMacFindFolder(short vRefNum
,
269 Boolean createFolder
);
272 #if wxUSE_GUI // GUI only things from now on
274 // ----------------------------------------------------------------------------
275 // Menu accelerators related things
276 // ----------------------------------------------------------------------------
278 WXDLLEXPORT wxChar
* wxStripMenuCodes(wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
279 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
282 class WXDLLEXPORT wxAcceleratorEntry
;
283 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
284 #endif // wxUSE_ACCEL
286 // ----------------------------------------------------------------------------
288 // ----------------------------------------------------------------------------
290 // Find the window/widget with the given title or label.
291 // Pass a parent to begin the search from, or NULL to look through
293 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
295 // Find window by name, and if that fails, by label.
296 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
298 // Returns menu item id or -1 if none.
299 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
301 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
302 // is always present but may be less reliable than a native version.
303 WXDLLEXPORT wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
);
304 WXDLLEXPORT wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
);
306 // ----------------------------------------------------------------------------
307 // Message/event queue helpers
308 // ----------------------------------------------------------------------------
310 // Yield to other apps/messages
311 WXDLLEXPORT
bool wxYield();
313 // Like wxYield, but fails silently if the yield is recursive.
314 WXDLLEXPORT
bool wxYieldIfNeeded();
316 // Yield to other apps/messages and disable user input
317 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
);
319 // Enable or disable input to all top level windows
320 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= TRUE
);
322 // Check whether this window wants to process messages, e.g. Stop button
323 // in long calculations.
324 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
326 // Consume all events until no more left
327 WXDLLEXPORT
void wxFlushEvents();
329 // a class which disables all windows (except, may be, thegiven one) in its
330 // ctor and enables them back in its dtor
331 class WXDLLEXPORT wxWindowDisabler
334 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
338 wxWindowList
*m_winDisabled
;
341 // ----------------------------------------------------------------------------
343 // ----------------------------------------------------------------------------
345 // Set the cursor to the busy cursor for all windows
346 class WXDLLEXPORT wxCursor
;
347 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
348 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
350 // Restore cursor to normal
351 WXDLLEXPORT
void wxEndBusyCursor();
353 // TRUE if we're between the above two calls
354 WXDLLEXPORT
bool wxIsBusy();
356 // Convenience class so we can just create a wxBusyCursor object on the stack
357 class WXDLLEXPORT wxBusyCursor
360 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
361 { wxBeginBusyCursor(cursor
); }
363 { wxEndBusyCursor(); }
365 // FIXME: These two methods are currently only implemented (and needed?)
366 // in wxGTK. BusyCursor handling should probably be moved to
367 // common code since the wxGTK and wxMSW implementations are very
368 // similar except for wxMSW using HCURSOR directly instead of
370 static const wxCursor
&GetStoredCursor();
371 static const wxCursor
GetBusyCursor();
375 // ----------------------------------------------------------------------------
376 // Reading and writing resources (eg WIN.INI, .Xdefaults)
377 // ----------------------------------------------------------------------------
380 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
381 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
382 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
383 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
385 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
386 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
387 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
388 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
389 #endif // wxUSE_RESOURCES
391 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
393 // MSW only: get user-defined resource from the .res file.
394 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
396 WXDLLEXPORT
extern const wxChar
* wxUserResourceStr
;
397 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
399 // Implemented in utils.cpp: VC++, Win95 only. Sets up a console for standard
401 WXDLLEXPORT
void wxRedirectIOToConsole();
405 // ----------------------------------------------------------------------------
406 // Display and colorss (X only)
407 // ----------------------------------------------------------------------------
410 void *wxGetDisplay();
414 WXDisplay
*wxGetDisplay();
415 bool wxSetDisplay(const wxString
& display_name
);
416 wxString
wxGetDisplayName();
421 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
422 // The resulting warnings are switched off here
423 #pragma message disable nosimpint
425 #include <X11/Xlib.h>
427 #pragma message enable nosimpint
430 #define wxMAX_RGB 0xff
431 #define wxMAX_SV 1000
432 #define wxSIGN(x) ((x < 0) ? -x : x)
437 typedef struct wx_hsv
{
441 #define wxMax3(x,y,z) ((x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z))
442 #define wxMin3(x,y,z) ((x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z))
444 void wxHSVToXColor(wxHSV
*hsv
,XColor
*xcolor
);
445 void wxXColorToHSV(wxHSV
*hsv
,XColor
*xcolor
);
446 void wxAllocNearestColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
447 void wxAllocColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
453 // ----------------------------------------------------------------------------
454 // Error message functions used by wxWindows (deprecated, use wxLog)
455 // ----------------------------------------------------------------------------
457 // Format a message on the standard error (UNIX) or the debugging
459 WXDLLEXPORT
void wxDebugMsg(const wxChar
*fmt
...);
461 // Non-fatal error (continues)
462 WXDLLEXPORT_DATA(extern const wxChar
*) wxInternalErrorStr
;
463 WXDLLEXPORT
void wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
465 // Fatal error (exits)
466 WXDLLEXPORT_DATA(extern const wxChar
*) wxFatalErrorStr
;
467 WXDLLEXPORT
void wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);