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(const wxString
& buf
);
96 // Convert decimal integer to 2-character hex string
97 void WXDLLEXPORT
wxDecToHex(int dec
, char *buf
);
98 wxString WXDLLEXPORT
wxDecToHex(int dec
);
100 // Execute another program. Returns 0 if there was an error, a PID otherwise.
101 long WXDLLEXPORT
wxExecute(char **argv
, bool sync
= FALSE
,
102 wxProcess
*process
= (wxProcess
*) NULL
);
103 long WXDLLEXPORT
wxExecute(const wxString
& command
, bool sync
= FALSE
,
104 wxProcess
*process
= (wxProcess
*) NULL
);
108 int WXDLLEXPORT
wxKill(long pid
, int sig
=wxSIGTERM
);
110 // Execute a command in an interactive shell window
111 // If no command then just the shell
112 bool WXDLLEXPORT
wxShell(const wxString
& command
= "");
114 // Sleep for nSecs seconds under UNIX, do nothing under Windows
115 void WXDLLEXPORT
wxSleep(int nSecs
);
117 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
118 long WXDLLEXPORT
wxGetFreeMemory(void);
120 // Consume all events until no more left
121 void WXDLLEXPORT
wxFlushEvents(void);
124 * Network and username functions.
129 bool WXDLLEXPORT
wxGetEmailAddress(char *buf
, int maxSize
);
132 bool WXDLLEXPORT
wxGetHostName(char *buf
, int maxSize
);
134 // Get user ID e.g. jacs
135 bool WXDLLEXPORT
wxGetUserId(char *buf
, int maxSize
);
137 // Get user name e.g. Julian Smart
138 bool WXDLLEXPORT
wxGetUserName(char *buf
, int maxSize
);
141 * Strip out any menu codes
143 char* WXDLLEXPORT
wxStripMenuCodes(char *in
, char *out
= (char *) NULL
);
145 // Find the window/widget with the given title or label.
146 // Pass a parent to begin the search from, or NULL to look through
148 wxWindow
* WXDLLEXPORT
wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
150 // Find window by name, and if that fails, by label.
151 wxWindow
* WXDLLEXPORT
wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
153 // Returns menu item id or -1 if none.
154 int WXDLLEXPORT
wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
156 // A debugging stream buffer.
157 // Under Windows, this writes to the Windows debug output.
158 // Under other platforms, it writes to cerr.
160 // ALl this horrible gubbins required for Borland, because the calling
161 // convention needs to be the same as for streambuf.
162 // Thanks to Gerhard.Vogt@embl-heidelberg.de for this solution.
164 #if defined(__BORLANDC__) && defined(__BCOPT__) && !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
168 // Can't export a class derived from a non-export class
169 #if !defined(_WINDLL) && !defined(WXUSINGDLL)
175 class WXDLLEXPORT wxDebugStreamBuf
: public streambuf
178 wxDebugStreamBuf(void);
179 ~wxDebugStreamBuf(void) {}
182 inline int underflow(void) { return EOF
; }
186 // #if WXDEBUG && wxUSE_GLOBAL_MEMORY_OPERATORS
187 // #define new WXDEBUG_NEW
192 #if defined(__BORLANDC__) && defined(__BCOPT__) && !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
197 #if (!defined(__MINMAX_DEFINED) && !defined(max))
198 #define max(a,b) (((a) > (b)) ? (a) : (b))
199 #define min(a,b) (((a) < (b)) ? (a) : (b))
200 #define __MINMAX_DEFINED 1
203 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
204 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
206 // Yield to other apps/messages
207 bool WXDLLEXPORT
wxYield(void);
209 // Format a message on the standard error (UNIX) or the debugging
211 void WXDLLEXPORT
wxDebugMsg(const char *fmt
...) ;
214 void WXDLLEXPORT
wxBell(void) ;
217 int WXDLLEXPORT
wxGetOsVersion(int *majorVsn
= (int *) NULL
,int *minorVsn
= (int *) NULL
) ;
219 // Set the cursor to the busy cursor for all windows
220 class WXDLLEXPORT wxCursor
;
221 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
222 void WXDLLEXPORT
wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
224 // Restore cursor to normal
225 void WXDLLEXPORT
wxEndBusyCursor(void);
227 // TRUE if we're between the above two calls
228 bool WXDLLEXPORT
wxIsBusy(void);
230 /* Error message functions used by wxWindows */
232 // Non-fatal error (continues)
233 WXDLLEXPORT_DATA(extern const char*) wxInternalErrorStr
;
234 void WXDLLEXPORT
wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
236 // Fatal error (exits)
237 WXDLLEXPORT_DATA(extern const char*) wxFatalErrorStr
;
238 void WXDLLEXPORT
wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);
240 // Reading and writing resources (eg WIN.INI, .Xdefaults)
242 bool WXDLLEXPORT
wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= "");
243 bool WXDLLEXPORT
wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= "");
244 bool WXDLLEXPORT
wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= "");
245 bool WXDLLEXPORT
wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= "");
247 bool WXDLLEXPORT
wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
= "");
248 bool WXDLLEXPORT
wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= "");
249 bool WXDLLEXPORT
wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= "");
250 bool WXDLLEXPORT
wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= "");
251 #endif // wxUSE_RESOURCES
253 // Get current Home dir and copy to dest (returns pstr->c_str())
254 const char* WXDLLEXPORT
wxGetHomeDir(wxString
*pstr
);
256 // Get the user's home dir (caller must copy--- volatile)
257 // returns NULL is no HOME dir is known
258 char* WXDLLEXPORT
wxGetUserHome(const wxString
& user
= "");
260 // Check whether this window wants to process messages, e.g. Stop button
261 // in long calculations.
262 bool WXDLLEXPORT
wxCheckForInterrupt(wxWindow
*wnd
);
264 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
266 // MSW only: get user-defined resource from the .res file.
267 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
269 extern const char* WXDLLEXPORT wxUserResourceStr
;
270 char* WXDLLEXPORT
wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
275 WXDisplay
*wxGetDisplay();
276 bool wxSetDisplay(const wxString
& display_name
);
277 wxString
wxGetDisplayName();
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
);