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
;
44 // FIXME should use wxStricmp() instead
45 #if defined(__GNUWIN32__)
46 #define stricmp strcasecmp
47 #define strnicmp strncasecmp
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
55 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
57 // ----------------------------------------------------------------------------
58 // String functions (deprecated, use wxString)
59 // ----------------------------------------------------------------------------
61 // Useful buffer (FIXME VZ: yeah, that is. To be removed!)
62 WXDLLEXPORT_DATA(extern wxChar
*) wxBuffer
;
64 // Make a copy of this string using 'new'
65 WXDLLEXPORT wxChar
* copystring(const wxChar
*s
);
67 // Matches string one within string two regardless of case
68 WXDLLEXPORT
bool StringMatch(wxChar
*one
, wxChar
*two
, bool subString
= TRUE
, bool exact
= FALSE
);
70 // A shorter way of using strcmp
71 #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
73 // ----------------------------------------------------------------------------
74 // Miscellaneous functions
75 // ----------------------------------------------------------------------------
78 WXDLLEXPORT
void wxBell();
80 // Get OS description as a user-readable string
81 WXDLLEXPORT wxString
wxGetOsDescription();
84 WXDLLEXPORT
int wxGetOsVersion(int *majorVsn
= (int *) NULL
,
85 int *minorVsn
= (int *) NULL
);
87 // Return a string with the current date/time
88 WXDLLEXPORT wxString
wxNow();
91 // Don't synthesize KeyUp events holding down a key and producing
92 // KeyDown events with autorepeat. On by default and always on
94 WXDLLEXPORT
bool wxSetDetectableAutoRepeat( bool flag
);
96 // ----------------------------------------------------------------------------
97 // Window ID management
98 // ----------------------------------------------------------------------------
100 // Generate a unique ID
101 WXDLLEXPORT
long wxNewId();
102 #if !defined(NewId) && defined(WXWIN_COMPATIBILITY)
103 #define NewId wxNewId
106 // Ensure subsequent IDs don't clash with this one
107 WXDLLEXPORT
void wxRegisterId(long id
);
108 #if !defined(RegisterId) && defined(WXWIN_COMPATIBILITY)
109 #define RegisterId wxRegisterId
112 // Return the current ID
113 WXDLLEXPORT
long wxGetCurrentId();
117 // ----------------------------------------------------------------------------
118 // Various conversions
119 // ----------------------------------------------------------------------------
121 WXDLLEXPORT_DATA(extern const wxChar
*) wxFloatToStringStr
;
122 WXDLLEXPORT_DATA(extern const wxChar
*) wxDoubleToStringStr
;
124 WXDLLEXPORT
void StringToFloat(wxChar
*s
, float *number
);
125 WXDLLEXPORT wxChar
* FloatToString(float number
, const wxChar
*fmt
= wxFloatToStringStr
);
126 WXDLLEXPORT
void StringToDouble(wxChar
*s
, double *number
);
127 WXDLLEXPORT wxChar
* DoubleToString(double number
, const wxChar
*fmt
= wxDoubleToStringStr
);
128 WXDLLEXPORT
void StringToInt(wxChar
*s
, int *number
);
129 WXDLLEXPORT
void StringToLong(wxChar
*s
, long *number
);
130 WXDLLEXPORT wxChar
* IntToString(int number
);
131 WXDLLEXPORT wxChar
* LongToString(long number
);
133 // Convert 2-digit hex number to decimal
134 WXDLLEXPORT
int wxHexToDec(const wxString
& buf
);
136 // Convert decimal integer to 2-character hex string
137 WXDLLEXPORT
void wxDecToHex(int dec
, wxChar
*buf
);
138 WXDLLEXPORT wxString
wxDecToHex(int dec
);
140 // ----------------------------------------------------------------------------
141 // Process management
142 // ----------------------------------------------------------------------------
144 // Execute another program. Returns 0 if there was an error, a PID otherwise.
145 WXDLLEXPORT
long wxExecute(wxChar
**argv
, bool sync
= FALSE
,
146 wxProcess
*process
= (wxProcess
*) NULL
);
147 WXDLLEXPORT
long wxExecute(const wxString
& command
, bool sync
= FALSE
,
148 wxProcess
*process
= (wxProcess
*) NULL
);
150 // execute the command capturing its output into an array line by line
151 WXDLLEXPORT
long wxExecute(const wxString
& command
,
152 wxArrayString
& output
);
154 // also capture stderr
155 WXDLLEXPORT
long wxExecute(const wxString
& command
,
156 wxArrayString
& output
,
157 wxArrayString
& error
);
161 wxSIGNONE
= 0, // verify if the process exists under Unix
168 wxSIGIOT
= wxSIGABRT
, // another name
179 // further signals are different in meaning between different Unix systems
182 // the argument is ignored under Windows - the process is always killed
183 WXDLLEXPORT
int wxKill(long pid
, wxSignal sig
= wxSIGTERM
);
185 // Execute a command in an interactive shell window (always synchronously)
186 // If no command then just the shell
187 WXDLLEXPORT
bool wxShell(const wxString
& command
= wxEmptyString
);
189 // As wxShell(), but must give a (non interactive) command and its output will
190 // be returned in output array
191 WXDLLEXPORT
bool wxShell(const wxString
& command
, wxArrayString
& output
);
193 // Sleep for nSecs seconds
194 WXDLLEXPORT
void wxSleep(int nSecs
);
196 // Sleep for a given amount of milliseconds
197 WXDLLEXPORT
void wxUsleep(unsigned long milliseconds
);
199 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
200 WXDLLEXPORT
long wxGetFreeMemory();
202 // should wxApp::OnFatalException() be called?
203 WXDLLEXPORT
bool wxHandleFatalExceptions(bool doit
= TRUE
);
205 // ----------------------------------------------------------------------------
206 // Network and username functions.
207 // ----------------------------------------------------------------------------
209 // NB: "char *" functions are deprecated, use wxString ones!
212 WXDLLEXPORT
bool wxGetEmailAddress(wxChar
*buf
, int maxSize
);
213 WXDLLEXPORT wxString
wxGetEmailAddress();
216 WXDLLEXPORT
bool wxGetHostName(wxChar
*buf
, int maxSize
);
217 WXDLLEXPORT wxString
wxGetHostName();
220 WXDLLEXPORT wxString
wxGetFullHostName();
221 WXDLLEXPORT
bool wxGetFullHostName(wxChar
*buf
, int maxSize
);
223 // Get user ID e.g. jacs (this is known as login name under Unix)
224 WXDLLEXPORT
bool wxGetUserId(wxChar
*buf
, int maxSize
);
225 WXDLLEXPORT wxString
wxGetUserId();
227 // Get user name e.g. Julian Smart
228 WXDLLEXPORT
bool wxGetUserName(wxChar
*buf
, int maxSize
);
229 WXDLLEXPORT wxString
wxGetUserName();
231 // Get current Home dir and copy to dest (returns pstr->c_str())
232 WXDLLEXPORT wxString
wxGetHomeDir();
233 WXDLLEXPORT
const wxChar
* wxGetHomeDir(wxString
*pstr
);
235 // Get the user's home dir (caller must copy --- volatile)
236 // returns NULL is no HOME dir is known
237 #if defined(__UNIX__) && wxUSE_UNICODE
238 WXDLLEXPORT
const wxMB2WXbuf
wxGetUserHome(const wxString
& user
= wxEmptyString
);
240 WXDLLEXPORT wxChar
* wxGetUserHome(const wxString
& user
= wxEmptyString
);
243 #if wxUSE_GUI // GUI only things from now on
245 // ----------------------------------------------------------------------------
246 // Menu accelerators related things
247 // ----------------------------------------------------------------------------
249 WXDLLEXPORT wxChar
* wxStripMenuCodes(wxChar
*in
, wxChar
*out
= (wxChar
*) NULL
);
250 WXDLLEXPORT wxString
wxStripMenuCodes(const wxString
& str
);
253 class WXDLLEXPORT wxAcceleratorEntry
;
254 WXDLLEXPORT wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
);
255 #endif // wxUSE_ACCEL
257 // ----------------------------------------------------------------------------
259 // ----------------------------------------------------------------------------
261 // Find the window/widget with the given title or label.
262 // Pass a parent to begin the search from, or NULL to look through
264 WXDLLEXPORT wxWindow
* wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
266 // Find window by name, and if that fails, by label.
267 WXDLLEXPORT wxWindow
* wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
269 // Returns menu item id or -1 if none.
270 WXDLLEXPORT
int wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
272 // ----------------------------------------------------------------------------
273 // Message/event queue helpers
274 // ----------------------------------------------------------------------------
276 // Yield to other apps/messages
277 WXDLLEXPORT
bool wxYield();
279 // Yield to other apps/messages and disable user input
280 WXDLLEXPORT
bool wxSafeYield(wxWindow
*win
= NULL
);
282 // Enable or disable input to all top level windows
283 WXDLLEXPORT
void wxEnableTopLevelWindows(bool enable
= TRUE
);
285 // Check whether this window wants to process messages, e.g. Stop button
286 // in long calculations.
287 WXDLLEXPORT
bool wxCheckForInterrupt(wxWindow
*wnd
);
289 // Consume all events until no more left
290 WXDLLEXPORT
void wxFlushEvents();
292 // a class which disables all windows (except, may be, thegiven one) in its
293 // ctor and enables them back in its dtor
294 class WXDLLEXPORT wxWindowDisabler
297 wxWindowDisabler(wxWindow
*winToSkip
= (wxWindow
*)NULL
);
301 wxWindowList
*m_winDisabled
;
308 // ----------------------------------------------------------------------------
310 // ----------------------------------------------------------------------------
312 // Set the cursor to the busy cursor for all windows
313 class WXDLLEXPORT wxCursor
;
314 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
315 WXDLLEXPORT
void wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
317 // Restore cursor to normal
318 WXDLLEXPORT
void wxEndBusyCursor();
320 // TRUE if we're between the above two calls
321 WXDLLEXPORT
bool wxIsBusy();
323 // Convenience class so we can just create a wxBusyCursor object on the stack
324 class WXDLLEXPORT wxBusyCursor
327 wxBusyCursor(wxCursor
* cursor
= wxHOURGLASS_CURSOR
)
328 { wxBeginBusyCursor(cursor
); }
330 { wxEndBusyCursor(); }
332 // FIXME: These two methods are currently only implemented (and needed?)
333 // in wxGTK. BusyCursor handling should probably be moved to
334 // common code since the wxGTK and wxMSW implementations are very
335 // similar except for wxMSW using HCURSOR directly instead of
337 static const wxCursor
&GetStoredCursor();
338 static const wxCursor
GetBusyCursor();
342 // ----------------------------------------------------------------------------
343 // Reading and writing resources (eg WIN.INI, .Xdefaults)
344 // ----------------------------------------------------------------------------
347 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= wxEmptyString
);
348 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= wxEmptyString
);
349 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= wxEmptyString
);
350 WXDLLEXPORT
bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= wxEmptyString
);
352 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
= wxEmptyString
);
353 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= wxEmptyString
);
354 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= wxEmptyString
);
355 WXDLLEXPORT
bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= wxEmptyString
);
356 #endif // wxUSE_RESOURCES
358 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
360 // MSW only: get user-defined resource from the .res file.
361 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
363 WXDLLEXPORT
extern const wxChar
* wxUserResourceStr
;
364 WXDLLEXPORT wxChar
* wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
366 // Implemented in utils.cpp: VC++, Win95 only. Sets up a console for standard
368 WXDLLEXPORT
void wxRedirectIOToConsole();
372 // ----------------------------------------------------------------------------
373 // Display and colorss (X only)
374 // ----------------------------------------------------------------------------
377 void *wxGetDisplay();
381 WXDisplay
*wxGetDisplay();
382 bool wxSetDisplay(const wxString
& display_name
);
383 wxString
wxGetDisplayName();
388 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
389 // The resulting warnings are switched off here
390 #pragma message disable nosimpint
392 #include <X11/Xlib.h>
394 #pragma message enable nosimpint
397 #define wxMAX_RGB 0xff
398 #define wxMAX_SV 1000
399 #define wxSIGN(x) ((x < 0) ? -x : x)
404 typedef struct wx_hsv
{
408 #define wxMax3(x,y,z) ((x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z))
409 #define wxMin3(x,y,z) ((x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z))
411 void wxHSVToXColor(wxHSV
*hsv
,XColor
*xcolor
);
412 void wxXColorToHSV(wxHSV
*hsv
,XColor
*xcolor
);
413 void wxAllocNearestColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
414 void wxAllocColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
420 // ----------------------------------------------------------------------------
421 // Error message functions used by wxWindows (deprecated, use wxLog)
422 // ----------------------------------------------------------------------------
424 // Format a message on the standard error (UNIX) or the debugging
426 WXDLLEXPORT
void wxDebugMsg(const wxChar
*fmt
...);
428 // Non-fatal error (continues)
429 WXDLLEXPORT_DATA(extern const wxChar
*) wxInternalErrorStr
;
430 WXDLLEXPORT
void wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
432 // Fatal error (exits)
433 WXDLLEXPORT_DATA(extern const wxChar
*) wxFatalErrorStr
;
434 WXDLLEXPORT
void wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);