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