1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Miscellaneous utilities
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "utils.h"
20 #include "wx/object.h"
22 #include "wx/window.h"
23 #include "wx/filefn.h"
24 #include "wx/process.h"
34 /*steve: these two are not known under VMS */
43 #define stricmp strcasecmp
44 #define strnicmp strncasecmp
47 // Forward declaration
48 class WXDLLEXPORT wxFrame
;
50 // Stupid ASCII macros
51 #define wxToUpper(C) (((C) >= 'a' && (C) <= 'z')? (C) - 'a' + 'A': (C))
52 #define wxToLower(C) (((C) >= 'A' && (C) <= 'Z')? (C) - 'A' + 'a': (C))
54 // Return a string with the current date/time
55 wxString WXDLLEXPORT
wxNow(void);
57 // Make a copy of this string using 'new'
58 char* WXDLLEXPORT
copystring(const char *s
);
60 // Generate a unique ID
61 long WXDLLEXPORT
wxNewId(void);
64 // Ensure subsequent IDs don't clash with this one
65 void WXDLLEXPORT
wxRegisterId(long id
);
66 #define RegisterId wxRegisterId
68 // Return the current ID
69 long WXDLLEXPORT
wxGetCurrentId(void);
72 WXDLLEXPORT_DATA(extern char*) wxBuffer
;
74 WXDLLEXPORT_DATA(extern const char*) wxFloatToStringStr
;
75 WXDLLEXPORT_DATA(extern const char*) wxDoubleToStringStr
;
77 // Various conversions
78 void WXDLLEXPORT
StringToFloat(char *s
, float *number
);
79 char* WXDLLEXPORT
FloatToString(float number
, const char *fmt
= wxFloatToStringStr
);
80 void WXDLLEXPORT
StringToDouble(char *s
, double *number
);
81 char* WXDLLEXPORT
DoubleToString(double number
, const char *fmt
= wxDoubleToStringStr
);
82 void WXDLLEXPORT
StringToInt(char *s
, int *number
);
83 void WXDLLEXPORT
StringToLong(char *s
, long *number
);
84 char* WXDLLEXPORT
IntToString(int number
);
85 char* WXDLLEXPORT
LongToString(long number
);
87 // Matches string one within string two regardless of case
88 bool WXDLLEXPORT
StringMatch(char *one
, char *two
, bool subString
= TRUE
, bool exact
= FALSE
);
90 // A shorter way of using strcmp
91 #define wxStringEq(s1, s2) (s1 && s2 && (strcmp(s1, s2) == 0))
93 // Convert 2-digit hex number to decimal
94 int WXDLLEXPORT
wxHexToDec(char *buf
);
96 // Convert decimal integer to 2-character hex string
97 void WXDLLEXPORT
wxDecToHex(int dec
, char *buf
);
99 // Execute another program. Returns 0 if there was an error, a PID otherwise.
100 long WXDLLEXPORT
wxExecute(char **argv
, bool Async
= FALSE
,
101 wxProcess
*process
= NULL
);
102 long WXDLLEXPORT
wxExecute(const wxString
& command
, bool Async
= FALSE
,
103 wxProcess
*process
= NULL
);
107 int WXDLLEXPORT
wxKill(long pid
, int sig
=wxSIGTERM
);
109 // Execute a command in an interactive shell window
110 // If no command then just the shell
111 bool WXDLLEXPORT
wxShell(const wxString
& command
= "");
113 // Sleep for nSecs seconds under UNIX, do nothing under Windows
114 void WXDLLEXPORT
wxSleep(int nSecs
);
116 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
117 long WXDLLEXPORT
wxGetFreeMemory(void);
119 // Consume all events until no more left
120 void WXDLLEXPORT
wxFlushEvents(void);
123 * Network and username functions.
128 bool WXDLLEXPORT
wxGetEmailAddress(char *buf
, int maxSize
);
131 bool WXDLLEXPORT
wxGetHostName(char *buf
, int maxSize
);
133 // Get user ID e.g. jacs
134 bool WXDLLEXPORT
wxGetUserId(char *buf
, int maxSize
);
136 // Get user name e.g. Julian Smart
137 bool WXDLLEXPORT
wxGetUserName(char *buf
, int maxSize
);
140 * Strip out any menu codes
142 char* WXDLLEXPORT
wxStripMenuCodes(char *in
, char *out
= NULL
);
144 // Find the window/widget with the given title or label.
145 // Pass a parent to begin the search from, or NULL to look through
147 wxWindow
* WXDLLEXPORT
wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= NULL
);
149 // Find window by name, and if that fails, by label.
150 wxWindow
* WXDLLEXPORT
wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= NULL
);
152 // Returns menu item id or -1 if none.
153 int WXDLLEXPORT
wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
155 // A debugging stream buffer.
156 // Under Windows, this writes to the Windows debug output.
157 // Under other platforms, it writes to cerr.
159 // ALl this horrible gubbins required for Borland, because the calling
160 // convention needs to be the same as for streambuf.
161 // Thanks to Gerhard.Vogt@embl-heidelberg.de for this solution.
163 #if defined(__BORLANDC__) && defined(__BCOPT__) && !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
167 // Can't export a class derived from a non-export class
168 #if !defined(_WINDLL) && !defined(WXUSINGDLL)
174 class WXDLLEXPORT wxDebugStreamBuf
: public streambuf
177 wxDebugStreamBuf(void);
178 ~wxDebugStreamBuf(void) {}
181 inline int underflow(void) { return EOF
; }
185 #if DEBUG && USE_GLOBAL_MEMORY_OPERATORS
186 #define new WXDEBUG_NEW
191 #if defined(__BORLANDC__) && defined(__BCOPT__) && !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
196 #if (!defined(__MINMAX_DEFINED) && !defined(max))
197 #define max(a,b) (((a) > (b)) ? (a) : (b))
198 #define min(a,b) (((a) < (b)) ? (a) : (b))
199 #define __MINMAX_DEFINED 1
202 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
203 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
205 // Yield to other apps/messages
206 bool WXDLLEXPORT
wxYield(void);
208 // Format a message on the standard error (UNIX) or the debugging
210 void WXDLLEXPORT
wxDebugMsg(const char *fmt
...) ;
213 void WXDLLEXPORT
wxBell(void) ;
216 int WXDLLEXPORT
wxGetOsVersion(int *majorVsn
=NULL
,int *minorVsn
=NULL
) ;
218 // Set the cursor to the busy cursor for all windows
219 class WXDLLEXPORT wxCursor
;
220 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
221 void WXDLLEXPORT
wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
223 // Restore cursor to normal
224 void WXDLLEXPORT
wxEndBusyCursor(void);
226 // TRUE if we're between the above two calls
227 bool WXDLLEXPORT
wxIsBusy(void);
229 /* Error message functions used by wxWindows */
231 // Non-fatal error (continues)
232 WXDLLEXPORT_DATA(extern const char*) wxInternalErrorStr
;
233 void WXDLLEXPORT
wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
235 // Fatal error (exits)
236 WXDLLEXPORT_DATA(extern const char*) wxFatalErrorStr
;
237 void WXDLLEXPORT
wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);
239 // Reading and writing resources (eg WIN.INI, .Xdefaults)
241 bool WXDLLEXPORT
wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= "");
242 bool WXDLLEXPORT
wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= "");
243 bool WXDLLEXPORT
wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= "");
244 bool WXDLLEXPORT
wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= "");
246 bool WXDLLEXPORT
wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
= "");
247 bool WXDLLEXPORT
wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= "");
248 bool WXDLLEXPORT
wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= "");
249 bool WXDLLEXPORT
wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= "");
250 #endif // USE_RESOURCES
253 // 'X' Only, will soon vanish....
254 // Get current Home dir and copy to dest
255 char* WXDLLEXPORT
wxGetHomeDir( char *dest
);
257 // Get the user's home dir (caller must copy--- volatile)
258 // returns NULL is no HOME dir is known
259 char* WXDLLEXPORT
wxGetUserHome(const wxString
& user
= "");
261 // Check whether this window wants to process messages, e.g. Stop button
262 // in long calculations.
263 bool WXDLLEXPORT
wxCheckForInterrupt(wxWindow
*wnd
);
265 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
267 // MSW only: get user-defined resource from the .res file.
268 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
270 extern const char* WXDLLEXPORT wxUserResourceStr
;
271 char* WXDLLEXPORT
wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
276 // Get X display: often needed in the wxWindows implementation.
277 Display
*wxGetDisplay(void);
278 /* Matthew Flatt: Added wxSetDisplay and wxGetDisplayName */
279 bool wxSetDisplay(const wxString
& display_name
);
280 wxString
wxGetDisplayName(void);
285 #include <X11/Xlib.h>
287 #define wxMAX_RGB 0xff
288 #define wxMAX_SV 1000
289 #define wxSIGN(x) ((x < 0) ? -x : x)
294 typedef struct wx_hsv
{
298 #define wxMax3(x,y,z) ((x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z))
299 #define wxMin3(x,y,z) ((x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z))
301 #define wxMax2(x,y) ((x > y) ? x : y)
302 #define wxMin2(x,y) ((x < y) ? x : y)
304 void wxHSVToXColor(wxHSV
*hsv
,XColor
*xcolor
);
305 void wxXColorToHSV(wxHSV
*hsv
,XColor
*xcolor
);
306 void wxAllocNearestColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
307 void wxAllocColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);