Initial revision
[wxWidgets.git] / include / wx / utils.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.h
3 // Purpose: Miscellaneous utilities
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef __UTILSH__
13 #define __UTILSH__
14
15 #ifdef __GNUG__
16 #pragma interface "utils.h"
17 #endif
18
19 #include "wx/setup.h"
20 #include "wx/object.h"
21 #include "wx/list.h"
22 #include "wx/window.h"
23 #include "wx/filefn.h"
24
25 #if USE_IOSTREAMH
26 #include <iostream.h>
27 #else
28 #include <iostream>
29 #endif
30
31 #ifdef __X__
32 #ifndef __VMS__
33 /*steve: these two are not known under VMS */
34 #include <dirent.h>
35 #include <unistd.h>
36 #endif
37 #endif
38
39 #include <stdio.h>
40
41 #ifdef __GNUWIN32__
42 #define stricmp strcasecmp
43 #define strnicmp strncasecmp
44 #endif
45
46 // Forward declaration
47 class WXDLLEXPORT wxFrame;
48
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))
52
53 // Return a string with the current date/time
54 wxString WXDLLEXPORT wxNow(void);
55
56 // Make a copy of this string using 'new'
57 char* WXDLLEXPORT copystring(const char *s);
58
59 // Generate a unique ID
60 long WXDLLEXPORT wxNewId(void);
61 #define NewId wxNewId
62
63 // Ensure subsequent IDs don't clash with this one
64 void WXDLLEXPORT wxRegisterId(long id);
65 #define RegisterId wxRegisterId
66
67 // Return the current ID
68 long WXDLLEXPORT wxGetCurrentId(void);
69
70 // Useful buffer
71 WXDLLEXPORT_DATA(extern char*) wxBuffer;
72
73 WXDLLEXPORT_DATA(extern const char*) wxFloatToStringStr;
74 WXDLLEXPORT_DATA(extern const char*) wxDoubleToStringStr;
75
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);
85
86 // Matches string one within string two regardless of case
87 bool WXDLLEXPORT StringMatch(char *one, char *two, bool subString = TRUE, bool exact = FALSE);
88
89 // A shorter way of using strcmp
90 #define wxStringEq(s1, s2) (s1 && s2 && (strcmp(s1, s2) == 0))
91
92 // Convert 2-digit hex number to decimal
93 int WXDLLEXPORT wxHexToDec(char *buf);
94
95 // Convert decimal integer to 2-character hex string
96 void WXDLLEXPORT wxDecToHex(int dec, char *buf);
97
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);
101
102 #define wxSIGTERM 1
103
104 int WXDLLEXPORT wxKill(long pid, int sig=wxSIGTERM);
105
106 // Execute a command in an interactive shell window
107 // If no command then just the shell
108 bool WXDLLEXPORT wxShell(const wxString& command = "");
109
110 // Sleep for nSecs seconds under UNIX, do nothing under Windows
111 void WXDLLEXPORT wxSleep(int nSecs);
112
113 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
114 long WXDLLEXPORT wxGetFreeMemory(void);
115
116 // Consume all events until no more left
117 void WXDLLEXPORT wxFlushEvents(void);
118
119 /*
120 * Network and username functions.
121 *
122 */
123
124 // Get eMail address
125 bool WXDLLEXPORT wxGetEmailAddress(char *buf, int maxSize);
126
127 // Get hostname.
128 bool WXDLLEXPORT wxGetHostName(char *buf, int maxSize);
129
130 // Get user ID e.g. jacs
131 bool WXDLLEXPORT wxGetUserId(char *buf, int maxSize);
132
133 // Get user name e.g. Julian Smart
134 bool WXDLLEXPORT wxGetUserName(char *buf, int maxSize);
135
136 /*
137 * Strip out any menu codes
138 */
139 char* WXDLLEXPORT wxStripMenuCodes(char *in, char *out = NULL);
140
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
143 // all windows.
144 wxWindow* WXDLLEXPORT wxFindWindowByLabel(const wxString& title, wxWindow *parent = NULL);
145
146 // Find window by name, and if that fails, by label.
147 wxWindow* WXDLLEXPORT wxFindWindowByName(const wxString& name, wxWindow *parent = NULL);
148
149 // Returns menu item id or -1 if none.
150 int WXDLLEXPORT wxFindMenuItemId(wxFrame *frame, const wxString& menuString, const wxString& itemString);
151
152 // A debugging stream buffer.
153 // Under Windows, this writes to the Windows debug output.
154 // Under other platforms, it writes to cerr.
155
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.
159
160 #if defined(__BORLANDC__) && defined(__BCOPT__) && !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
161 #pragma option -po-
162 #endif
163
164 // Can't export a class derived from a non-export class
165 #if !defined(_WINDLL) && !defined(WXUSINGDLL)
166
167 #ifdef new
168 #undef new
169 #endif
170
171 class WXDLLEXPORT wxDebugStreamBuf: public streambuf
172 {
173 public:
174 wxDebugStreamBuf(void);
175 ~wxDebugStreamBuf(void) {}
176
177 int overflow(int i);
178 inline int underflow(void) { return EOF; }
179 int sync(void);
180 };
181
182 #if DEBUG && USE_GLOBAL_MEMORY_OPERATORS
183 #define new WXDEBUG_NEW
184 #endif
185
186 #endif
187
188 #if defined(__BORLANDC__) && defined(__BCOPT__) && !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
189 #pragma option -po.
190 #endif
191
192 /*
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
197 #endif
198 */
199 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
200 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
201
202 // Yield to other apps/messages
203 bool WXDLLEXPORT wxYield(void);
204
205 // Format a message on the standard error (UNIX) or the debugging
206 // stream (Windows)
207 void WXDLLEXPORT wxDebugMsg(const char *fmt ...) ;
208
209 // Sound the bell
210 void WXDLLEXPORT wxBell(void) ;
211
212 // Get OS version
213 int WXDLLEXPORT wxGetOsVersion(int *majorVsn=NULL,int *minorVsn=NULL) ;
214
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);
219
220 // Restore cursor to normal
221 void WXDLLEXPORT wxEndBusyCursor(void);
222
223 // TRUE if we're between the above two calls
224 bool WXDLLEXPORT wxIsBusy(void);
225
226 /* Error message functions used by wxWindows */
227
228 // Non-fatal error (continues)
229 WXDLLEXPORT_DATA(extern const char*) wxInternalErrorStr;
230 void WXDLLEXPORT wxError(const wxString& msg, const wxString& title = wxInternalErrorStr);
231
232 // Fatal error (exits)
233 WXDLLEXPORT_DATA(extern const char*) wxFatalErrorStr;
234 void WXDLLEXPORT wxFatalError(const wxString& msg, const wxString& title = wxFatalErrorStr);
235
236 // Reading and writing resources (eg WIN.INI, .Xdefaults)
237 #if USE_RESOURCES
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 = "");
242
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
248
249 #ifdef __UNIX__
250 // 'X' Only, will soon vanish....
251 // Get current Home dir and copy to dest
252 char* WXDLLEXPORT wxGetHomeDir( char *dest );
253 #endif
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 = "");
257
258 // Check whether this window wants to process messages, e.g. Stop button
259 // in long calculations.
260 bool WXDLLEXPORT wxCheckForInterrupt(wxWindow *wnd);
261
262 void WXDLLEXPORT wxGetMousePosition( int* x, int* y );
263
264 // MSW only: get user-defined resource from the .res file.
265 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
266 #ifdef __WINDOWS__
267 extern const char* WXDLLEXPORT wxUserResourceStr;
268 char* WXDLLEXPORT wxLoadUserResource(const wxString& resourceName, const wxString& resourceType = wxUserResourceStr);
269 #endif
270
271 // X only
272 #ifdef __X__
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);
278 #endif
279
280 #ifdef __X__
281
282 #include <X11/Xlib.h>
283
284 #define wxMAX_RGB 0xff
285 #define wxMAX_SV 1000
286 #define wxSIGN(x) ((x < 0) ? -x : x)
287 #define wxH_WEIGHT 4
288 #define wxS_WEIGHT 1
289 #define wxV_WEIGHT 2
290
291 typedef struct wx_hsv {
292 int h,s,v;
293 } wxHSV;
294
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))
297
298 #define wxMax2(x,y) ((x > y) ? x : y)
299 #define wxMin2(x,y) ((x < y) ? x : y)
300
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);
305
306 #endif //__X__
307
308 #endif
309 // __UTILSH__