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"
33 /*steve: these two are not known under VMS */
42 #define stricmp strcasecmp
43 #define strnicmp strncasecmp
46 // Forward declaration
47 class WXDLLEXPORT wxFrame
;
49 // Stupid ASCII macros
50 #define wxToUpper(C) (((C) >= 'a' && (C) <= 'z')? (C) - 'a' + 'A': (C))
51 #define wxToLower(C) (((C) >= 'A' && (C) <= 'Z')? (C) - 'A' + 'a': (C))
53 // Return a string with the current date/time
54 wxString WXDLLEXPORT
wxNow(void);
56 // Make a copy of this string using 'new'
57 char* WXDLLEXPORT
copystring(const char *s
);
59 // Generate a unique ID
60 long WXDLLEXPORT
wxNewId(void);
63 // Ensure subsequent IDs don't clash with this one
64 void WXDLLEXPORT
wxRegisterId(long id
);
65 #define RegisterId wxRegisterId
67 // Return the current ID
68 long WXDLLEXPORT
wxGetCurrentId(void);
71 WXDLLEXPORT_DATA(extern char*) wxBuffer
;
73 WXDLLEXPORT_DATA(extern const char*) wxFloatToStringStr
;
74 WXDLLEXPORT_DATA(extern const char*) wxDoubleToStringStr
;
76 // Various conversions
77 void WXDLLEXPORT
StringToFloat(char *s
, float *number
);
78 char* WXDLLEXPORT
FloatToString(float number
, const char *fmt
= wxFloatToStringStr
);
79 void WXDLLEXPORT
StringToDouble(char *s
, double *number
);
80 char* WXDLLEXPORT
DoubleToString(double number
, const char *fmt
= wxDoubleToStringStr
);
81 void WXDLLEXPORT
StringToInt(char *s
, int *number
);
82 void WXDLLEXPORT
StringToLong(char *s
, long *number
);
83 char* WXDLLEXPORT
IntToString(int number
);
84 char* WXDLLEXPORT
LongToString(long number
);
86 // Matches string one within string two regardless of case
87 bool WXDLLEXPORT
StringMatch(char *one
, char *two
, bool subString
= TRUE
, bool exact
= FALSE
);
89 // A shorter way of using strcmp
90 #define wxStringEq(s1, s2) (s1 && s2 && (strcmp(s1, s2) == 0))
92 // Convert 2-digit hex number to decimal
93 int WXDLLEXPORT
wxHexToDec(char *buf
);
95 // Convert decimal integer to 2-character hex string
96 void WXDLLEXPORT
wxDecToHex(int dec
, char *buf
);
98 // Execute another program. Returns 0 if there was an error, a PID otherwise.
99 long WXDLLEXPORT
wxExecute(char **argv
, bool Async
= FALSE
);
100 long WXDLLEXPORT
wxExecute(const wxString
& command
, bool Async
= FALSE
);
104 int WXDLLEXPORT
wxKill(long pid
, int sig
=wxSIGTERM
);
106 // Execute a command in an interactive shell window
107 // If no command then just the shell
108 bool WXDLLEXPORT
wxShell(const wxString
& command
= "");
110 // Sleep for nSecs seconds under UNIX, do nothing under Windows
111 void WXDLLEXPORT
wxSleep(int nSecs
);
113 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
114 long WXDLLEXPORT
wxGetFreeMemory(void);
116 // Consume all events until no more left
117 void WXDLLEXPORT
wxFlushEvents(void);
120 * Network and username functions.
125 bool WXDLLEXPORT
wxGetEmailAddress(char *buf
, int maxSize
);
128 bool WXDLLEXPORT
wxGetHostName(char *buf
, int maxSize
);
130 // Get user ID e.g. jacs
131 bool WXDLLEXPORT
wxGetUserId(char *buf
, int maxSize
);
133 // Get user name e.g. Julian Smart
134 bool WXDLLEXPORT
wxGetUserName(char *buf
, int maxSize
);
137 * Strip out any menu codes
139 char* WXDLLEXPORT
wxStripMenuCodes(char *in
, char *out
= NULL
);
141 // Find the window/widget with the given title or label.
142 // Pass a parent to begin the search from, or NULL to look through
144 wxWindow
* WXDLLEXPORT
wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= NULL
);
146 // Find window by name, and if that fails, by label.
147 wxWindow
* WXDLLEXPORT
wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= NULL
);
149 // Returns menu item id or -1 if none.
150 int WXDLLEXPORT
wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
152 // A debugging stream buffer.
153 // Under Windows, this writes to the Windows debug output.
154 // Under other platforms, it writes to cerr.
156 // ALl this horrible gubbins required for Borland, because the calling
157 // convention needs to be the same as for streambuf.
158 // Thanks to Gerhard.Vogt@embl-heidelberg.de for this solution.
160 #if defined(__BORLANDC__) && defined(__BCOPT__) && !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
164 // Can't export a class derived from a non-export class
165 #if !defined(_WINDLL) && !defined(WXUSINGDLL)
171 class WXDLLEXPORT wxDebugStreamBuf
: public streambuf
174 wxDebugStreamBuf(void);
175 ~wxDebugStreamBuf(void) {}
178 inline int underflow(void) { return EOF
; }
182 #if DEBUG && USE_GLOBAL_MEMORY_OPERATORS
183 #define new WXDEBUG_NEW
188 #if defined(__BORLANDC__) && defined(__BCOPT__) && !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
193 #if (!defined(__MINMAX_DEFINED) && !defined(max))
194 #define max(a,b) (((a) > (b)) ? (a) : (b))
195 #define min(a,b) (((a) < (b)) ? (a) : (b))
196 #define __MINMAX_DEFINED 1
199 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
200 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
202 // Yield to other apps/messages
203 bool WXDLLEXPORT
wxYield(void);
205 // Format a message on the standard error (UNIX) or the debugging
207 void WXDLLEXPORT
wxDebugMsg(const char *fmt
...) ;
210 void WXDLLEXPORT
wxBell(void) ;
213 int WXDLLEXPORT
wxGetOsVersion(int *majorVsn
=NULL
,int *minorVsn
=NULL
) ;
215 // Set the cursor to the busy cursor for all windows
216 class WXDLLEXPORT wxCursor
;
217 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
218 void WXDLLEXPORT
wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
220 // Restore cursor to normal
221 void WXDLLEXPORT
wxEndBusyCursor(void);
223 // TRUE if we're between the above two calls
224 bool WXDLLEXPORT
wxIsBusy(void);
226 /* Error message functions used by wxWindows */
228 // Non-fatal error (continues)
229 WXDLLEXPORT_DATA(extern const char*) wxInternalErrorStr
;
230 void WXDLLEXPORT
wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
232 // Fatal error (exits)
233 WXDLLEXPORT_DATA(extern const char*) wxFatalErrorStr
;
234 void WXDLLEXPORT
wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);
236 // Reading and writing resources (eg WIN.INI, .Xdefaults)
238 bool WXDLLEXPORT
wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= "");
239 bool WXDLLEXPORT
wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= "");
240 bool WXDLLEXPORT
wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= "");
241 bool WXDLLEXPORT
wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= "");
243 bool WXDLLEXPORT
wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
= "");
244 bool WXDLLEXPORT
wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= "");
245 bool WXDLLEXPORT
wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= "");
246 bool WXDLLEXPORT
wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= "");
247 #endif // USE_RESOURCES
250 // 'X' Only, will soon vanish....
251 // Get current Home dir and copy to dest
252 char* WXDLLEXPORT
wxGetHomeDir( char *dest
);
254 // Get the user's home dir (caller must copy--- volatile)
255 // returns NULL is no HOME dir is known
256 char* WXDLLEXPORT
wxGetUserHome(const wxString
& user
= "");
258 // Check whether this window wants to process messages, e.g. Stop button
259 // in long calculations.
260 bool WXDLLEXPORT
wxCheckForInterrupt(wxWindow
*wnd
);
262 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
264 // MSW only: get user-defined resource from the .res file.
265 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
267 extern const char* WXDLLEXPORT wxUserResourceStr
;
268 char* WXDLLEXPORT
wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
273 // Get X display: often needed in the wxWindows implementation.
274 Display
*wxGetDisplay(void);
275 /* Matthew Flatt: Added wxSetDisplay and wxGetDisplayName */
276 bool wxSetDisplay(const wxString
& display_name
);
277 wxString
wxGetDisplayName(void);
282 #include <X11/Xlib.h>
284 #define wxMAX_RGB 0xff
285 #define wxMAX_SV 1000
286 #define wxSIGN(x) ((x < 0) ? -x : x)
291 typedef struct wx_hsv
{
295 #define wxMax3(x,y,z) ((x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z))
296 #define wxMin3(x,y,z) ((x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z))
298 #define wxMax2(x,y) ((x > y) ? x : y)
299 #define wxMin2(x,y) ((x < y) ? x : y)
301 void wxHSVToXColor(wxHSV
*hsv
,XColor
*xcolor
);
302 void wxXColorToHSV(wxHSV
*hsv
,XColor
*xcolor
);
303 void wxAllocNearestColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
304 void wxAllocColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);