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