]>
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 | |
3f4a0c5b | 9 | // Licence: wxWindows license |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
34138703 JS |
12 | #ifndef _WX_UTILSH__ |
13 | #define _WX_UTILSH__ | |
c801d85f KB |
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 | ||
3f4a0c5b | 25 | #include "wx/ioswrap.h" |
c801d85f | 26 | |
1f916a19 | 27 | class WXDLLEXPORT wxProcess; |
18fd783c | 28 | |
c801d85f KB |
29 | #ifdef __X__ |
30 | #ifndef __VMS__ | |
31 | /*steve: these two are not known under VMS */ | |
32 | #include <dirent.h> | |
33 | #include <unistd.h> | |
34 | #endif | |
35 | #endif | |
36 | ||
37 | #include <stdio.h> | |
38 | ||
39 | #ifdef __GNUWIN32__ | |
40 | #define stricmp strcasecmp | |
41 | #define strnicmp strncasecmp | |
42 | #endif | |
43 | ||
44 | // Forward declaration | |
45 | class WXDLLEXPORT wxFrame; | |
46 | ||
47 | // Stupid ASCII macros | |
48 | #define wxToUpper(C) (((C) >= 'a' && (C) <= 'z')? (C) - 'a' + 'A': (C)) | |
49 | #define wxToLower(C) (((C) >= 'A' && (C) <= 'Z')? (C) - 'A' + 'a': (C)) | |
50 | ||
51 | // Return a string with the current date/time | |
184b5d99 | 52 | WXDLLEXPORT wxString wxNow(void); |
c801d85f KB |
53 | |
54 | // Make a copy of this string using 'new' | |
184b5d99 | 55 | WXDLLEXPORT char* copystring(const char *s); |
c801d85f KB |
56 | |
57 | // Generate a unique ID | |
184b5d99 | 58 | WXDLLEXPORT long wxNewId(void); |
c801d85f KB |
59 | #define NewId wxNewId |
60 | ||
61 | // Ensure subsequent IDs don't clash with this one | |
184b5d99 | 62 | WXDLLEXPORT void wxRegisterId(long id); |
c801d85f KB |
63 | #define RegisterId wxRegisterId |
64 | ||
65 | // Return the current ID | |
184b5d99 | 66 | WXDLLEXPORT long wxGetCurrentId(void); |
c801d85f KB |
67 | |
68 | // Useful buffer | |
69 | WXDLLEXPORT_DATA(extern char*) wxBuffer; | |
70 | ||
71 | WXDLLEXPORT_DATA(extern const char*) wxFloatToStringStr; | |
72 | WXDLLEXPORT_DATA(extern const char*) wxDoubleToStringStr; | |
73 | ||
74 | // Various conversions | |
184b5d99 JS |
75 | WXDLLEXPORT void StringToFloat(char *s, float *number); |
76 | WXDLLEXPORT char* FloatToString(float number, const char *fmt = wxFloatToStringStr); | |
77 | WXDLLEXPORT void StringToDouble(char *s, double *number); | |
78 | WXDLLEXPORT char* DoubleToString(double number, const char *fmt = wxDoubleToStringStr); | |
79 | WXDLLEXPORT void StringToInt(char *s, int *number); | |
80 | WXDLLEXPORT void StringToLong(char *s, long *number); | |
81 | WXDLLEXPORT char* IntToString(int number); | |
82 | WXDLLEXPORT char* LongToString(long number); | |
c801d85f KB |
83 | |
84 | // Matches string one within string two regardless of case | |
184b5d99 | 85 | WXDLLEXPORT bool StringMatch(char *one, char *two, bool subString = TRUE, bool exact = FALSE); |
c801d85f KB |
86 | |
87 | // A shorter way of using strcmp | |
88 | #define wxStringEq(s1, s2) (s1 && s2 && (strcmp(s1, s2) == 0)) | |
89 | ||
90 | // Convert 2-digit hex number to decimal | |
184b5d99 | 91 | WXDLLEXPORT int wxHexToDec(const wxString& buf); |
c801d85f KB |
92 | |
93 | // Convert decimal integer to 2-character hex string | |
184b5d99 JS |
94 | WXDLLEXPORT void wxDecToHex(int dec, char *buf); |
95 | WXDLLEXPORT wxString wxDecToHex(int dec); | |
c801d85f KB |
96 | |
97 | // Execute another program. Returns 0 if there was an error, a PID otherwise. | |
184b5d99 | 98 | WXDLLEXPORT long wxExecute(char **argv, bool sync = FALSE, |
c67daf87 | 99 | wxProcess *process = (wxProcess *) NULL); |
184b5d99 | 100 | WXDLLEXPORT long wxExecute(const wxString& command, bool sync = FALSE, |
c67daf87 | 101 | wxProcess *process = (wxProcess *) NULL); |
c801d85f KB |
102 | |
103 | #define wxSIGTERM 1 | |
104 | ||
184b5d99 | 105 | WXDLLEXPORT int wxKill(long pid, int sig=wxSIGTERM); |
c801d85f KB |
106 | |
107 | // Execute a command in an interactive shell window | |
108 | // If no command then just the shell | |
62448488 | 109 | WXDLLEXPORT bool wxShell(const wxString& command = wxEmptyString); |
c801d85f KB |
110 | |
111 | // Sleep for nSecs seconds under UNIX, do nothing under Windows | |
184b5d99 | 112 | WXDLLEXPORT void wxSleep(int nSecs); |
c801d85f KB |
113 | |
114 | // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) | |
184b5d99 | 115 | WXDLLEXPORT long wxGetFreeMemory(void); |
c801d85f KB |
116 | |
117 | // Consume all events until no more left | |
184b5d99 | 118 | WXDLLEXPORT void wxFlushEvents(void); |
c801d85f KB |
119 | |
120 | /* | |
121 | * Network and username functions. | |
122 | * | |
123 | */ | |
124 | ||
125 | // Get eMail address | |
184b5d99 | 126 | WXDLLEXPORT bool wxGetEmailAddress(char *buf, int maxSize); |
c801d85f KB |
127 | |
128 | // Get hostname. | |
184b5d99 | 129 | WXDLLEXPORT bool wxGetHostName(char *buf, int maxSize); |
e2a6f233 | 130 | WXDLLEXPORT bool wxGetHostName(wxString& buf); |
c801d85f KB |
131 | |
132 | // Get user ID e.g. jacs | |
184b5d99 | 133 | WXDLLEXPORT bool wxGetUserId(char *buf, int maxSize); |
e2a6f233 | 134 | WXDLLEXPORT bool wxGetUserId(wxString& buf); |
c801d85f KB |
135 | |
136 | // Get user name e.g. Julian Smart | |
184b5d99 | 137 | WXDLLEXPORT bool wxGetUserName(char *buf, int maxSize); |
e2a6f233 | 138 | WXDLLEXPORT bool wxGetUserName(wxString& buf); |
c801d85f KB |
139 | |
140 | /* | |
141 | * Strip out any menu codes | |
142 | */ | |
184b5d99 JS |
143 | WXDLLEXPORT char* wxStripMenuCodes(char *in, char *out = (char *) NULL); |
144 | WXDLLEXPORT wxString wxStripMenuCodes(const wxString& str); | |
c801d85f KB |
145 | |
146 | // Find the window/widget with the given title or label. | |
147 | // Pass a parent to begin the search from, or NULL to look through | |
148 | // all windows. | |
184b5d99 | 149 | WXDLLEXPORT wxWindow* wxFindWindowByLabel(const wxString& title, wxWindow *parent = (wxWindow *) NULL); |
c801d85f KB |
150 | |
151 | // Find window by name, and if that fails, by label. | |
184b5d99 | 152 | WXDLLEXPORT wxWindow* wxFindWindowByName(const wxString& name, wxWindow *parent = (wxWindow *) NULL); |
c801d85f KB |
153 | |
154 | // Returns menu item id or -1 if none. | |
184b5d99 | 155 | WXDLLEXPORT int wxFindMenuItemId(wxFrame *frame, const wxString& menuString, const wxString& itemString); |
c801d85f | 156 | |
c801d85f KB |
157 | /* |
158 | #if (!defined(__MINMAX_DEFINED) && !defined(max)) | |
159 | #define max(a,b) (((a) > (b)) ? (a) : (b)) | |
160 | #define min(a,b) (((a) < (b)) ? (a) : (b)) | |
161 | #define __MINMAX_DEFINED 1 | |
162 | #endif | |
163 | */ | |
d524867f | 164 | |
c801d85f KB |
165 | #define wxMax(a,b) (((a) > (b)) ? (a) : (b)) |
166 | #define wxMin(a,b) (((a) < (b)) ? (a) : (b)) | |
167 | ||
168 | // Yield to other apps/messages | |
184b5d99 | 169 | WXDLLEXPORT bool wxYield(void); |
c801d85f KB |
170 | |
171 | // Format a message on the standard error (UNIX) or the debugging | |
172 | // stream (Windows) | |
184b5d99 | 173 | WXDLLEXPORT void wxDebugMsg(const char *fmt ...) ; |
c801d85f KB |
174 | |
175 | // Sound the bell | |
184b5d99 | 176 | WXDLLEXPORT void wxBell(void) ; |
c801d85f KB |
177 | |
178 | // Get OS version | |
184b5d99 | 179 | WXDLLEXPORT int wxGetOsVersion(int *majorVsn= (int *) NULL,int *minorVsn= (int *) NULL) ; |
c801d85f KB |
180 | |
181 | // Set the cursor to the busy cursor for all windows | |
182 | class WXDLLEXPORT wxCursor; | |
183 | WXDLLEXPORT_DATA(extern wxCursor*) wxHOURGLASS_CURSOR; | |
184b5d99 | 184 | WXDLLEXPORT void wxBeginBusyCursor(wxCursor *cursor = wxHOURGLASS_CURSOR); |
e2a6f233 | 185 | |
c801d85f | 186 | // Restore cursor to normal |
184b5d99 | 187 | WXDLLEXPORT void wxEndBusyCursor(void); |
c801d85f KB |
188 | |
189 | // TRUE if we're between the above two calls | |
184b5d99 | 190 | WXDLLEXPORT bool wxIsBusy(void); |
c801d85f | 191 | |
e2a6f233 JS |
192 | // Convenience class so we can just create a wxBusyCursor object on the stack |
193 | class WXDLLEXPORT wxBusyCursor | |
194 | { | |
816e2c28 | 195 | public: |
e2a6f233 JS |
196 | inline wxBusyCursor(wxCursor* cursor = wxHOURGLASS_CURSOR) { wxBeginBusyCursor(cursor); } |
197 | inline ~wxBusyCursor() { wxEndBusyCursor(); } | |
198 | }; | |
199 | ||
c801d85f KB |
200 | /* Error message functions used by wxWindows */ |
201 | ||
202 | // Non-fatal error (continues) | |
203 | WXDLLEXPORT_DATA(extern const char*) wxInternalErrorStr; | |
184b5d99 | 204 | WXDLLEXPORT void wxError(const wxString& msg, const wxString& title = wxInternalErrorStr); |
c801d85f KB |
205 | |
206 | // Fatal error (exits) | |
207 | WXDLLEXPORT_DATA(extern const char*) wxFatalErrorStr; | |
184b5d99 | 208 | WXDLLEXPORT void wxFatalError(const wxString& msg, const wxString& title = wxFatalErrorStr); |
c801d85f KB |
209 | |
210 | // Reading and writing resources (eg WIN.INI, .Xdefaults) | |
47d67540 | 211 | #if wxUSE_RESOURCES |
62448488 JS |
212 | WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file = wxEmptyString); |
213 | WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file = wxEmptyString); | |
214 | WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file = wxEmptyString); | |
215 | WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file = wxEmptyString); | |
216 | ||
217 | WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file = wxEmptyString); | |
218 | WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file = wxEmptyString); | |
219 | WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file = wxEmptyString); | |
220 | WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file = wxEmptyString); | |
47d67540 | 221 | #endif // wxUSE_RESOURCES |
c801d85f | 222 | |
7c3c5ecc | 223 | // Get current Home dir and copy to dest (returns pstr->c_str()) |
184b5d99 | 224 | WXDLLEXPORT const char* wxGetHomeDir(wxString *pstr); |
7c3c5ecc | 225 | |
c801d85f KB |
226 | // Get the user's home dir (caller must copy--- volatile) |
227 | // returns NULL is no HOME dir is known | |
62448488 | 228 | WXDLLEXPORT char* wxGetUserHome(const wxString& user = wxEmptyString); |
c801d85f KB |
229 | |
230 | // Check whether this window wants to process messages, e.g. Stop button | |
231 | // in long calculations. | |
184b5d99 | 232 | WXDLLEXPORT bool wxCheckForInterrupt(wxWindow *wnd); |
c801d85f KB |
233 | |
234 | void WXDLLEXPORT wxGetMousePosition( int* x, int* y ); | |
235 | ||
236 | // MSW only: get user-defined resource from the .res file. | |
237 | // Returns NULL or newly-allocated memory, so use delete[] to clean up. | |
2049ba38 | 238 | #ifdef __WXMSW__ |
184b5d99 JS |
239 | WXDLLEXPORT extern const char* wxUserResourceStr; |
240 | WXDLLEXPORT char* wxLoadUserResource(const wxString& resourceName, const wxString& resourceType = wxUserResourceStr); | |
c030b70f JS |
241 | |
242 | // Implemented in utils.cpp: VC++, Win95 only. Sets up a console for standard | |
243 | // input/output | |
244 | WXDLLEXPORT void wxRedirectIOToConsole(); | |
245 | ||
c801d85f KB |
246 | #endif |
247 | ||
248 | // X only | |
249 | #ifdef __X__ | |
46ccb510 | 250 | WXDisplay *wxGetDisplay(); |
c801d85f | 251 | bool wxSetDisplay(const wxString& display_name); |
46ccb510 | 252 | wxString wxGetDisplayName(); |
c801d85f KB |
253 | #endif |
254 | ||
255 | #ifdef __X__ | |
256 | ||
257 | #include <X11/Xlib.h> | |
258 | ||
259 | #define wxMAX_RGB 0xff | |
260 | #define wxMAX_SV 1000 | |
261 | #define wxSIGN(x) ((x < 0) ? -x : x) | |
262 | #define wxH_WEIGHT 4 | |
263 | #define wxS_WEIGHT 1 | |
264 | #define wxV_WEIGHT 2 | |
265 | ||
266 | typedef struct wx_hsv { | |
267 | int h,s,v; | |
268 | } wxHSV; | |
269 | ||
270 | #define wxMax3(x,y,z) ((x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z)) | |
271 | #define wxMin3(x,y,z) ((x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z)) | |
272 | ||
273 | #define wxMax2(x,y) ((x > y) ? x : y) | |
274 | #define wxMin2(x,y) ((x < y) ? x : y) | |
275 | ||
276 | void wxHSVToXColor(wxHSV *hsv,XColor *xcolor); | |
277 | void wxXColorToHSV(wxHSV *hsv,XColor *xcolor); | |
278 | void wxAllocNearestColor(Display *display,Colormap colormap,XColor *xcolor); | |
279 | void wxAllocColor(Display *display,Colormap colormap,XColor *xcolor); | |
280 | ||
281 | #endif //__X__ | |
282 | ||
283 | #endif | |
34138703 | 284 | // _WX_UTILSH__ |