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"
37 /*steve: these two are not known under VMS */
46 #define stricmp strcasecmp
47 #define strnicmp strncasecmp
50 // Forward declaration
51 class WXDLLEXPORT wxFrame
;
53 // Stupid ASCII macros
54 #define wxToUpper(C) (((C) >= 'a' && (C) <= 'z')? (C) - 'a' + 'A': (C))
55 #define wxToLower(C) (((C) >= 'A' && (C) <= 'Z')? (C) - 'A' + 'a': (C))
57 // Return a string with the current date/time
58 wxString WXDLLEXPORT
wxNow(void);
60 // Make a copy of this string using 'new'
61 char* WXDLLEXPORT
copystring(const char *s
);
63 // Generate a unique ID
64 long WXDLLEXPORT
wxNewId(void);
67 // Ensure subsequent IDs don't clash with this one
68 void WXDLLEXPORT
wxRegisterId(long id
);
69 #define RegisterId wxRegisterId
71 // Return the current ID
72 long WXDLLEXPORT
wxGetCurrentId(void);
75 WXDLLEXPORT_DATA(extern char*) wxBuffer
;
77 WXDLLEXPORT_DATA(extern const char*) wxFloatToStringStr
;
78 WXDLLEXPORT_DATA(extern const char*) wxDoubleToStringStr
;
80 // Various conversions
81 void WXDLLEXPORT
StringToFloat(char *s
, float *number
);
82 char* WXDLLEXPORT
FloatToString(float number
, const char *fmt
= wxFloatToStringStr
);
83 void WXDLLEXPORT
StringToDouble(char *s
, double *number
);
84 char* WXDLLEXPORT
DoubleToString(double number
, const char *fmt
= wxDoubleToStringStr
);
85 void WXDLLEXPORT
StringToInt(char *s
, int *number
);
86 void WXDLLEXPORT
StringToLong(char *s
, long *number
);
87 char* WXDLLEXPORT
IntToString(int number
);
88 char* WXDLLEXPORT
LongToString(long number
);
90 // Matches string one within string two regardless of case
91 bool WXDLLEXPORT
StringMatch(char *one
, char *two
, bool subString
= TRUE
, bool exact
= FALSE
);
93 // A shorter way of using strcmp
94 #define wxStringEq(s1, s2) (s1 && s2 && (strcmp(s1, s2) == 0))
96 // Convert 2-digit hex number to decimal
97 int WXDLLEXPORT
wxHexToDec(const wxString
& buf
);
99 // Convert decimal integer to 2-character hex string
100 void WXDLLEXPORT
wxDecToHex(int dec
, char *buf
);
101 wxString WXDLLEXPORT
wxDecToHex(int dec
);
103 // Execute another program. Returns 0 if there was an error, a PID otherwise.
104 long WXDLLEXPORT
wxExecute(char **argv
, bool sync
= FALSE
,
105 wxProcess
*process
= (wxProcess
*) NULL
);
106 long WXDLLEXPORT
wxExecute(const wxString
& command
, bool sync
= FALSE
,
107 wxProcess
*process
= (wxProcess
*) NULL
);
111 int WXDLLEXPORT
wxKill(long pid
, int sig
=wxSIGTERM
);
113 // Execute a command in an interactive shell window
114 // If no command then just the shell
115 bool WXDLLEXPORT
wxShell(const wxString
& command
= "");
117 // Sleep for nSecs seconds under UNIX, do nothing under Windows
118 void WXDLLEXPORT
wxSleep(int nSecs
);
120 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
121 long WXDLLEXPORT
wxGetFreeMemory(void);
123 // Consume all events until no more left
124 void WXDLLEXPORT
wxFlushEvents(void);
127 * Network and username functions.
132 bool WXDLLEXPORT
wxGetEmailAddress(char *buf
, int maxSize
);
135 bool WXDLLEXPORT
wxGetHostName(char *buf
, int maxSize
);
137 // Get user ID e.g. jacs
138 bool WXDLLEXPORT
wxGetUserId(char *buf
, int maxSize
);
140 // Get user name e.g. Julian Smart
141 bool WXDLLEXPORT
wxGetUserName(char *buf
, int maxSize
);
144 * Strip out any menu codes
146 char* WXDLLEXPORT
wxStripMenuCodes(char *in
, char *out
= (char *) NULL
);
147 wxString WXDLLEXPORT
wxStripMenuCodes(const wxString
& str
);
149 // Find the window/widget with the given title or label.
150 // Pass a parent to begin the search from, or NULL to look through
152 wxWindow
* WXDLLEXPORT
wxFindWindowByLabel(const wxString
& title
, wxWindow
*parent
= (wxWindow
*) NULL
);
154 // Find window by name, and if that fails, by label.
155 wxWindow
* WXDLLEXPORT
wxFindWindowByName(const wxString
& name
, wxWindow
*parent
= (wxWindow
*) NULL
);
157 // Returns menu item id or -1 if none.
158 int WXDLLEXPORT
wxFindMenuItemId(wxFrame
*frame
, const wxString
& menuString
, const wxString
& itemString
);
160 // A debugging stream buffer.
161 // Under Windows, this writes to the Windows debug output.
162 // Under other platforms, it writes to cerr.
164 // ALl this horrible gubbins required for Borland, because the calling
165 // convention needs to be the same as for streambuf.
166 // Thanks to Gerhard.Vogt@embl-heidelberg.de for this solution.
168 #if defined(__BORLANDC__) && defined(__BCOPT__) && !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
172 // Can't export a class derived from a non-export class
173 #if !defined(_WINDLL) && !defined(WXUSINGDLL)
179 class WXDLLEXPORT wxDebugStreamBuf
: public streambuf
182 wxDebugStreamBuf(void);
183 ~wxDebugStreamBuf(void) {}
186 inline int underflow(void) { return EOF
; }
190 // #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS
191 // #define new WXDEBUG_NEW
196 #if defined(__BORLANDC__) && defined(__BCOPT__) && !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
201 #if (!defined(__MINMAX_DEFINED) && !defined(max))
202 #define max(a,b) (((a) > (b)) ? (a) : (b))
203 #define min(a,b) (((a) < (b)) ? (a) : (b))
204 #define __MINMAX_DEFINED 1
207 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
208 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
210 // Yield to other apps/messages
211 bool WXDLLEXPORT
wxYield(void);
213 // Format a message on the standard error (UNIX) or the debugging
215 void WXDLLEXPORT
wxDebugMsg(const char *fmt
...) ;
218 void WXDLLEXPORT
wxBell(void) ;
221 int WXDLLEXPORT
wxGetOsVersion(int *majorVsn
= (int *) NULL
,int *minorVsn
= (int *) NULL
) ;
223 // Set the cursor to the busy cursor for all windows
224 class WXDLLEXPORT wxCursor
;
225 WXDLLEXPORT_DATA(extern wxCursor
*) wxHOURGLASS_CURSOR
;
226 void WXDLLEXPORT
wxBeginBusyCursor(wxCursor
*cursor
= wxHOURGLASS_CURSOR
);
228 // Restore cursor to normal
229 void WXDLLEXPORT
wxEndBusyCursor(void);
231 // TRUE if we're between the above two calls
232 bool WXDLLEXPORT
wxIsBusy(void);
234 /* Error message functions used by wxWindows */
236 // Non-fatal error (continues)
237 WXDLLEXPORT_DATA(extern const char*) wxInternalErrorStr
;
238 void WXDLLEXPORT
wxError(const wxString
& msg
, const wxString
& title
= wxInternalErrorStr
);
240 // Fatal error (exits)
241 WXDLLEXPORT_DATA(extern const char*) wxFatalErrorStr
;
242 void WXDLLEXPORT
wxFatalError(const wxString
& msg
, const wxString
& title
= wxFatalErrorStr
);
244 // Reading and writing resources (eg WIN.INI, .Xdefaults)
246 bool WXDLLEXPORT
wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
= "");
247 bool WXDLLEXPORT
wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
= "");
248 bool WXDLLEXPORT
wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
= "");
249 bool WXDLLEXPORT
wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
= "");
251 bool WXDLLEXPORT
wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
= "");
252 bool WXDLLEXPORT
wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
= "");
253 bool WXDLLEXPORT
wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
= "");
254 bool WXDLLEXPORT
wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
= "");
255 #endif // wxUSE_RESOURCES
257 // Get current Home dir and copy to dest (returns pstr->c_str())
258 const char* WXDLLEXPORT
wxGetHomeDir(wxString
*pstr
);
260 // Get the user's home dir (caller must copy--- volatile)
261 // returns NULL is no HOME dir is known
262 char* WXDLLEXPORT
wxGetUserHome(const wxString
& user
= "");
264 // Check whether this window wants to process messages, e.g. Stop button
265 // in long calculations.
266 bool WXDLLEXPORT
wxCheckForInterrupt(wxWindow
*wnd
);
268 void WXDLLEXPORT
wxGetMousePosition( int* x
, int* y
);
270 // MSW only: get user-defined resource from the .res file.
271 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
273 extern const char* WXDLLEXPORT wxUserResourceStr
;
274 char* WXDLLEXPORT
wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
= wxUserResourceStr
);
279 WXDisplay
*wxGetDisplay();
280 bool wxSetDisplay(const wxString
& display_name
);
281 wxString
wxGetDisplayName();
286 #include <X11/Xlib.h>
288 #define wxMAX_RGB 0xff
289 #define wxMAX_SV 1000
290 #define wxSIGN(x) ((x < 0) ? -x : x)
295 typedef struct wx_hsv
{
299 #define wxMax3(x,y,z) ((x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z))
300 #define wxMin3(x,y,z) ((x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z))
302 #define wxMax2(x,y) ((x > y) ? x : y)
303 #define wxMin2(x,y) ((x < y) ? x : y)
305 void wxHSVToXColor(wxHSV
*hsv
,XColor
*xcolor
);
306 void wxXColorToHSV(wxHSV
*hsv
,XColor
*xcolor
);
307 void wxAllocNearestColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);
308 void wxAllocColor(Display
*display
,Colormap colormap
,XColor
*xcolor
);