]> git.saurik.com Git - wxWidgets.git/blob - include/wx/utils.h
A bugfix in the wxTreeCtrl.GetItem wrapper
[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 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #ifdef __GNUG__
20 #pragma interface "utils.h"
21 #endif
22
23 #include "wx/setup.h"
24 #include "wx/object.h"
25 #include "wx/list.h"
26 #include "wx/filefn.h"
27
28 #ifdef __X__
29 #include <dirent.h>
30 #include <unistd.h>
31 #endif
32
33 #include <stdio.h>
34
35 // ----------------------------------------------------------------------------
36 // Forward declaration
37 // ----------------------------------------------------------------------------
38
39 class WXDLLEXPORT wxProcess;
40 class WXDLLEXPORT wxFrame;
41 class WXDLLEXPORT wxWindow;
42
43 // FIXME should use wxStricmp() instead
44 #if defined(__GNUWIN32__)
45 #define stricmp strcasecmp
46 #define strnicmp strncasecmp
47 #endif
48
49 // ----------------------------------------------------------------------------
50 // Macros
51 // ----------------------------------------------------------------------------
52
53 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
54 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
55
56 // ----------------------------------------------------------------------------
57 // String functions (deprecated, use wxString)
58 // ----------------------------------------------------------------------------
59
60 // Useful buffer (FIXME VZ: yeah, that is. To be removed!)
61 WXDLLEXPORT_DATA(extern wxChar*) wxBuffer;
62
63 // Make a copy of this string using 'new'
64 WXDLLEXPORT wxChar* copystring(const wxChar *s);
65
66 // Matches string one within string two regardless of case
67 WXDLLEXPORT bool StringMatch(wxChar *one, wxChar *two, bool subString = TRUE, bool exact = FALSE);
68
69 // A shorter way of using strcmp
70 #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
71
72 // ----------------------------------------------------------------------------
73 // Miscellaneous functions
74 // ----------------------------------------------------------------------------
75
76 // Sound the bell
77 WXDLLEXPORT void wxBell();
78
79 // Get OS description as a user-readable string
80 WXDLLEXPORT wxString wxGetOsDescription();
81
82 // Get OS version
83 WXDLLEXPORT int wxGetOsVersion(int *majorVsn = (int *) NULL,
84 int *minorVsn = (int *) NULL);
85
86 // Return a string with the current date/time
87 WXDLLEXPORT wxString wxNow();
88
89 #if wxUSE_GUI
90 // Don't synthesize KeyUp events holding down a key and producing
91 // KeyDown events with autorepeat. On by default and always on
92 // in wxMSW.
93 WXDLLEXPORT bool wxSetDetectableAutoRepeat( bool flag );
94
95 // ----------------------------------------------------------------------------
96 // Window ID management
97 // ----------------------------------------------------------------------------
98
99 // Generate a unique ID
100 WXDLLEXPORT long wxNewId();
101 #if !defined(NewId) && defined(WXWIN_COMPATIBILITY)
102 #define NewId wxNewId
103 #endif
104
105 // Ensure subsequent IDs don't clash with this one
106 WXDLLEXPORT void wxRegisterId(long id);
107 #if !defined(RegisterId) && defined(WXWIN_COMPATIBILITY)
108 #define RegisterId wxRegisterId
109 #endif
110
111 // Return the current ID
112 WXDLLEXPORT long wxGetCurrentId();
113
114 #endif // wxUSE_GUI
115
116 // ----------------------------------------------------------------------------
117 // Various conversions
118 // ----------------------------------------------------------------------------
119
120 WXDLLEXPORT_DATA(extern const wxChar*) wxFloatToStringStr;
121 WXDLLEXPORT_DATA(extern const wxChar*) wxDoubleToStringStr;
122
123 WXDLLEXPORT void StringToFloat(wxChar *s, float *number);
124 WXDLLEXPORT wxChar* FloatToString(float number, const wxChar *fmt = wxFloatToStringStr);
125 WXDLLEXPORT void StringToDouble(wxChar *s, double *number);
126 WXDLLEXPORT wxChar* DoubleToString(double number, const wxChar *fmt = wxDoubleToStringStr);
127 WXDLLEXPORT void StringToInt(wxChar *s, int *number);
128 WXDLLEXPORT void StringToLong(wxChar *s, long *number);
129 WXDLLEXPORT wxChar* IntToString(int number);
130 WXDLLEXPORT wxChar* LongToString(long number);
131
132 // Convert 2-digit hex number to decimal
133 WXDLLEXPORT int wxHexToDec(const wxString& buf);
134
135 // Convert decimal integer to 2-character hex string
136 WXDLLEXPORT void wxDecToHex(int dec, wxChar *buf);
137 WXDLLEXPORT wxString wxDecToHex(int dec);
138
139 // ----------------------------------------------------------------------------
140 // Process management
141 // ----------------------------------------------------------------------------
142
143 // Execute another program. Returns 0 if there was an error, a PID otherwise.
144 WXDLLEXPORT long wxExecute(wxChar **argv, bool sync = FALSE,
145 wxProcess *process = (wxProcess *) NULL);
146 WXDLLEXPORT long wxExecute(const wxString& command, bool sync = FALSE,
147 wxProcess *process = (wxProcess *) NULL);
148
149 enum wxSignal
150 {
151 wxSIGNONE = 0, // verify if the process exists under Unix
152 wxSIGHUP,
153 wxSIGINT,
154 wxSIGQUIT,
155 wxSIGILL,
156 wxSIGTRAP,
157 wxSIGABRT,
158 wxSIGIOT = wxSIGABRT, // another name
159 wxSIGEMT,
160 wxSIGFPE,
161 wxSIGKILL,
162 wxSIGBUS,
163 wxSIGSEGV,
164 wxSIGSYS,
165 wxSIGPIPE,
166 wxSIGALRM,
167 wxSIGTERM
168
169 // further signals are different in meaning between different Unix systems
170 };
171
172 // the argument is ignored under Windows - the process is always killed
173 WXDLLEXPORT int wxKill(long pid, wxSignal sig = wxSIGTERM);
174
175 // Execute a command in an interactive shell window
176 // If no command then just the shell
177 WXDLLEXPORT bool wxShell(const wxString& command = wxEmptyString);
178
179 // Sleep for nSecs seconds
180 WXDLLEXPORT void wxSleep(int nSecs);
181
182 // Sleep for a given amount of milliseconds
183 WXDLLEXPORT void wxUsleep(unsigned long milliseconds);
184
185 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
186 WXDLLEXPORT long wxGetFreeMemory();
187
188 // ----------------------------------------------------------------------------
189 // Network and username functions.
190 // ----------------------------------------------------------------------------
191
192 // NB: "char *" functions are deprecated, use wxString ones!
193
194 // Get eMail address
195 WXDLLEXPORT bool wxGetEmailAddress(wxChar *buf, int maxSize);
196 WXDLLEXPORT wxString wxGetEmailAddress();
197
198 // Get hostname.
199 WXDLLEXPORT bool wxGetHostName(wxChar *buf, int maxSize);
200 WXDLLEXPORT wxString wxGetHostName();
201
202 // Get FQDN
203 WXDLLEXPORT wxString wxGetFullHostName();
204 WXDLLEXPORT bool wxGetFullHostName(wxChar *buf, int maxSize);
205
206 // Get user ID e.g. jacs (this is known as login name under Unix)
207 WXDLLEXPORT bool wxGetUserId(wxChar *buf, int maxSize);
208 WXDLLEXPORT wxString wxGetUserId();
209
210 // Get user name e.g. Julian Smart
211 WXDLLEXPORT bool wxGetUserName(wxChar *buf, int maxSize);
212 WXDLLEXPORT wxString wxGetUserName();
213
214 // Get current Home dir and copy to dest (returns pstr->c_str())
215 WXDLLEXPORT wxString wxGetHomeDir();
216 WXDLLEXPORT const wxChar* wxGetHomeDir(wxString *pstr);
217
218 // Get the user's home dir (caller must copy --- volatile)
219 // returns NULL is no HOME dir is known
220 #if defined(__UNIX__) && wxUSE_UNICODE
221 WXDLLEXPORT const wxMB2WXbuf wxGetUserHome(const wxString& user = wxEmptyString);
222 #else
223 WXDLLEXPORT wxChar* wxGetUserHome(const wxString& user = wxEmptyString);
224 #endif
225
226 #if wxUSE_GUI // GUI only things from now on
227
228 // ----------------------------------------------------------------------------
229 // Menu accelerators related things
230 // ----------------------------------------------------------------------------
231
232 WXDLLEXPORT wxChar* wxStripMenuCodes(wxChar *in, wxChar *out = (wxChar *) NULL);
233 WXDLLEXPORT wxString wxStripMenuCodes(const wxString& str);
234
235 #if wxUSE_ACCEL
236 class WXDLLEXPORT wxAcceleratorEntry;
237 WXDLLEXPORT wxAcceleratorEntry *wxGetAccelFromString(const wxString& label);
238 #endif // wxUSE_ACCEL
239
240 // ----------------------------------------------------------------------------
241 // Window search
242 // ----------------------------------------------------------------------------
243
244 // Find the window/widget with the given title or label.
245 // Pass a parent to begin the search from, or NULL to look through
246 // all windows.
247 WXDLLEXPORT wxWindow* wxFindWindowByLabel(const wxString& title, wxWindow *parent = (wxWindow *) NULL);
248
249 // Find window by name, and if that fails, by label.
250 WXDLLEXPORT wxWindow* wxFindWindowByName(const wxString& name, wxWindow *parent = (wxWindow *) NULL);
251
252 // Returns menu item id or -1 if none.
253 WXDLLEXPORT int wxFindMenuItemId(wxFrame *frame, const wxString& menuString, const wxString& itemString);
254
255 // ----------------------------------------------------------------------------
256 // Message/event queue helpers
257 // ----------------------------------------------------------------------------
258
259 // Yield to other apps/messages
260 WXDLLEXPORT bool wxYield();
261
262 // Yield to other apps/messages and disable user input
263 WXDLLEXPORT bool wxSafeYield(wxWindow *win = NULL);
264
265 // Enable or disable input to all top level windows
266 WXDLLEXPORT void wxEnableTopLevelWindows(bool enable = TRUE);
267
268 // Check whether this window wants to process messages, e.g. Stop button
269 // in long calculations.
270 WXDLLEXPORT bool wxCheckForInterrupt(wxWindow *wnd);
271
272 // Consume all events until no more left
273 WXDLLEXPORT void wxFlushEvents();
274
275 // ----------------------------------------------------------------------------
276 // Cursors
277 // ----------------------------------------------------------------------------
278
279 // Set the cursor to the busy cursor for all windows
280 class WXDLLEXPORT wxCursor;
281 WXDLLEXPORT_DATA(extern wxCursor*) wxHOURGLASS_CURSOR;
282 WXDLLEXPORT void wxBeginBusyCursor(wxCursor *cursor = wxHOURGLASS_CURSOR);
283
284 // Restore cursor to normal
285 WXDLLEXPORT void wxEndBusyCursor();
286
287 // TRUE if we're between the above two calls
288 WXDLLEXPORT bool wxIsBusy();
289
290 // Convenience class so we can just create a wxBusyCursor object on the stack
291 class WXDLLEXPORT wxBusyCursor
292 {
293 public:
294 wxBusyCursor(wxCursor* cursor = wxHOURGLASS_CURSOR)
295 { wxBeginBusyCursor(cursor); }
296 ~wxBusyCursor()
297 { wxEndBusyCursor(); }
298 };
299
300 // ----------------------------------------------------------------------------
301 // Error message functions used by wxWindows (deprecated, use wxLog)
302 // ----------------------------------------------------------------------------
303
304 // Format a message on the standard error (UNIX) or the debugging
305 // stream (Windows)
306 WXDLLEXPORT void wxDebugMsg(const wxChar *fmt ...);
307
308 // Non-fatal error (continues)
309 WXDLLEXPORT_DATA(extern const wxChar*) wxInternalErrorStr;
310 WXDLLEXPORT void wxError(const wxString& msg, const wxString& title = wxInternalErrorStr);
311
312 // Fatal error (exits)
313 WXDLLEXPORT_DATA(extern const wxChar*) wxFatalErrorStr;
314 WXDLLEXPORT void wxFatalError(const wxString& msg, const wxString& title = wxFatalErrorStr);
315
316 // ----------------------------------------------------------------------------
317 // Reading and writing resources (eg WIN.INI, .Xdefaults)
318 // ----------------------------------------------------------------------------
319
320 #if wxUSE_RESOURCES
321 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file = wxEmptyString);
322 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file = wxEmptyString);
323 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file = wxEmptyString);
324 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file = wxEmptyString);
325
326 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file = wxEmptyString);
327 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file = wxEmptyString);
328 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file = wxEmptyString);
329 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file = wxEmptyString);
330 #endif // wxUSE_RESOURCES
331
332 void WXDLLEXPORT wxGetMousePosition( int* x, int* y );
333
334 // MSW only: get user-defined resource from the .res file.
335 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
336 #ifdef __WXMSW__
337 WXDLLEXPORT extern const wxChar* wxUserResourceStr;
338 WXDLLEXPORT wxChar* wxLoadUserResource(const wxString& resourceName, const wxString& resourceType = wxUserResourceStr);
339
340 // Implemented in utils.cpp: VC++, Win95 only. Sets up a console for standard
341 // input/output
342 WXDLLEXPORT void wxRedirectIOToConsole();
343
344 #endif // MSW
345
346 // ----------------------------------------------------------------------------
347 // Display and colorss (X only)
348 // ----------------------------------------------------------------------------
349
350 #ifdef __WXGTK__
351 void *wxGetDisplay();
352 #endif
353
354 #ifdef __X__
355 WXDisplay *wxGetDisplay();
356 bool wxSetDisplay(const wxString& display_name);
357 wxString wxGetDisplayName();
358 #endif // X or GTK+
359
360 #ifdef __X__
361
362 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
363 // The resulting warnings are switched off here
364 #pragma message disable nosimpint
365 #endif
366 #include <X11/Xlib.h>
367 #ifdef __VMS__
368 #pragma message enable nosimpint
369 #endif
370
371 #define wxMAX_RGB 0xff
372 #define wxMAX_SV 1000
373 #define wxSIGN(x) ((x < 0) ? -x : x)
374 #define wxH_WEIGHT 4
375 #define wxS_WEIGHT 1
376 #define wxV_WEIGHT 2
377
378 typedef struct wx_hsv {
379 int h,s,v;
380 } wxHSV;
381
382 #define wxMax3(x,y,z) ((x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z))
383 #define wxMin3(x,y,z) ((x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z))
384
385 void wxHSVToXColor(wxHSV *hsv,XColor *xcolor);
386 void wxXColorToHSV(wxHSV *hsv,XColor *xcolor);
387 void wxAllocNearestColor(Display *display,Colormap colormap,XColor *xcolor);
388 void wxAllocColor(Display *display,Colormap colormap,XColor *xcolor);
389
390 #endif //__X__
391
392 #endif // wxUSE_GUI
393
394 #endif
395 // _WX_UTILSH__