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