]> git.saurik.com Git - wxWidgets.git/blob - include/wx/utils.h
FreeBSD patch to sckaddr.cpp; include more files in manual; misc
[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 _WX_UTILSH__
13 #define _WX_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 #include "wx/process.h"
25
26 #if wxUSE_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.
100 long WXDLLEXPORT wxExecute(char **argv, bool sync = FALSE,
101 wxProcess *process = (wxProcess *) NULL);
102 long WXDLLEXPORT wxExecute(const wxString& command, bool sync = FALSE,
103 wxProcess *process = (wxProcess *) NULL);
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 = (char *) 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 = (wxWindow *) NULL);
148
149 // Find window by name, and if that fails, by label.
150 wxWindow* WXDLLEXPORT wxFindWindowByName(const wxString& name, wxWindow *parent = (wxWindow *) 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 WXDEBUG && wxUSE_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= (int *) NULL,int *minorVsn= (int *) 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 wxUSE_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 // wxUSE_RESOURCES
251
252 // Get current Home dir and copy to dest (returns pstr->c_str())
253 const char* WXDLLEXPORT wxGetHomeDir(wxString *pstr);
254
255 // Get the user's home dir (caller must copy--- volatile)
256 // returns NULL is no HOME dir is known
257 char* WXDLLEXPORT wxGetUserHome(const wxString& user = "");
258
259 // Check whether this window wants to process messages, e.g. Stop button
260 // in long calculations.
261 bool WXDLLEXPORT wxCheckForInterrupt(wxWindow *wnd);
262
263 void WXDLLEXPORT wxGetMousePosition( int* x, int* y );
264
265 // MSW only: get user-defined resource from the .res file.
266 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
267 #ifdef __WXMSW__
268 extern const char* WXDLLEXPORT wxUserResourceStr;
269 char* WXDLLEXPORT wxLoadUserResource(const wxString& resourceName, const wxString& resourceType = wxUserResourceStr);
270 #endif
271
272 // X only
273 #ifdef __X__
274 WXDisplay *wxGetDisplay();
275 bool wxSetDisplay(const wxString& display_name);
276 wxString wxGetDisplayName();
277 #endif
278
279 #ifdef __X__
280
281 #include <X11/Xlib.h>
282
283 #define wxMAX_RGB 0xff
284 #define wxMAX_SV 1000
285 #define wxSIGN(x) ((x < 0) ? -x : x)
286 #define wxH_WEIGHT 4
287 #define wxS_WEIGHT 1
288 #define wxV_WEIGHT 2
289
290 typedef struct wx_hsv {
291 int h,s,v;
292 } wxHSV;
293
294 #define wxMax3(x,y,z) ((x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z))
295 #define wxMin3(x,y,z) ((x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z))
296
297 #define wxMax2(x,y) ((x > y) ? x : y)
298 #define wxMin2(x,y) ((x < y) ? x : y)
299
300 void wxHSVToXColor(wxHSV *hsv,XColor *xcolor);
301 void wxXColorToHSV(wxHSV *hsv,XColor *xcolor);
302 void wxAllocNearestColor(Display *display,Colormap colormap,XColor *xcolor);
303 void wxAllocColor(Display *display,Colormap colormap,XColor *xcolor);
304
305 #endif //__X__
306
307 #endif
308 // _WX_UTILSH__