]> git.saurik.com Git - wxWidgets.git/blob - include/wx/utils.h
use generic timer in wxMGL and wxX11
[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/object.h"
24 #include "wx/list.h"
25 #include "wx/filefn.h"
26
27 // need this for wxGetDiskSpace() as we can't, unfortunately, forward declare
28 // wxLongLong
29 #include "wx/longlong.h"
30
31 #ifdef __X__
32 #include <dirent.h>
33 #include <unistd.h>
34 #endif
35
36 #include <stdio.h>
37
38 // ----------------------------------------------------------------------------
39 // Forward declaration
40 // ----------------------------------------------------------------------------
41
42 class WXDLLEXPORT wxProcess;
43 class WXDLLEXPORT wxFrame;
44 class WXDLLEXPORT wxWindow;
45 class WXDLLEXPORT wxWindowList;
46 class WXDLLEXPORT wxPoint;
47
48 // ----------------------------------------------------------------------------
49 // Macros
50 // ----------------------------------------------------------------------------
51
52 #define wxMax(a,b) (((a) > (b)) ? (a) : (b))
53 #define wxMin(a,b) (((a) < (b)) ? (a) : (b))
54
55 // ----------------------------------------------------------------------------
56 // String functions (deprecated, use wxString)
57 // ----------------------------------------------------------------------------
58
59 // Useful buffer (FIXME VZ: To be removed!!!)
60 // Now only needed in Mac and MSW ports
61 #if !defined(__WXMOTIF__) && !defined(__WXGTK__) && !defined(__WXX11__) && !defined(__WXMGL__)
62 WXDLLEXPORT_DATA(extern wxChar*) wxBuffer;
63 #endif
64
65 // Make a copy of this string using 'new'
66 WXDLLEXPORT wxChar* copystring(const wxChar *s);
67
68 // Matches string one within string two regardless of case
69 WXDLLEXPORT bool StringMatch(const wxChar *one, const wxChar *two, bool subString = TRUE, bool exact = FALSE);
70
71 // A shorter way of using strcmp
72 #define wxStringEq(s1, s2) (s1 && s2 && (wxStrcmp(s1, s2) == 0))
73
74 // ----------------------------------------------------------------------------
75 // Miscellaneous functions
76 // ----------------------------------------------------------------------------
77
78 // Sound the bell
79 WXDLLEXPORT void wxBell();
80
81 // Get OS description as a user-readable string
82 WXDLLEXPORT wxString wxGetOsDescription();
83
84 // Get OS version
85 WXDLLEXPORT int wxGetOsVersion(int *majorVsn = (int *) NULL,
86 int *minorVsn = (int *) NULL);
87
88 // Return a string with the current date/time
89 WXDLLEXPORT wxString wxNow();
90
91 // Return path where wxWindows is installed (mostly useful in Unices)
92 WXDLLEXPORT const wxChar *wxGetInstallPrefix();
93 // Return path to wxWin data (/usr/share/wx/%{version}) (Unices)
94 WXDLLEXPORT wxString wxGetDataDir();
95
96
97 #if wxUSE_GUI
98 // Don't synthesize KeyUp events holding down a key and producing
99 // KeyDown events with autorepeat. On by default and always on
100 // in wxMSW.
101 WXDLLEXPORT bool wxSetDetectableAutoRepeat( bool flag );
102
103 // ----------------------------------------------------------------------------
104 // Window ID management
105 // ----------------------------------------------------------------------------
106
107 // Generate a unique ID
108 WXDLLEXPORT long wxNewId();
109 #if !defined(NewId) && defined(WXWIN_COMPATIBILITY)
110 #define NewId wxNewId
111 #endif
112
113 // Ensure subsequent IDs don't clash with this one
114 WXDLLEXPORT void wxRegisterId(long id);
115 #if !defined(RegisterId) && defined(WXWIN_COMPATIBILITY)
116 #define RegisterId wxRegisterId
117 #endif
118
119 // Return the current ID
120 WXDLLEXPORT long wxGetCurrentId();
121
122 #endif // wxUSE_GUI
123
124 // ----------------------------------------------------------------------------
125 // Various conversions
126 // ----------------------------------------------------------------------------
127
128 WXDLLEXPORT_DATA(extern const wxChar*) wxFloatToStringStr;
129 WXDLLEXPORT_DATA(extern const wxChar*) wxDoubleToStringStr;
130
131 WXDLLEXPORT void StringToFloat(const wxChar *s, float *number);
132 WXDLLEXPORT wxChar* FloatToString(float number, const wxChar *fmt = wxFloatToStringStr);
133 WXDLLEXPORT void StringToDouble(const wxChar *s, double *number);
134 WXDLLEXPORT wxChar* DoubleToString(double number, const wxChar *fmt = wxDoubleToStringStr);
135 WXDLLEXPORT void StringToInt(const wxChar *s, int *number);
136 WXDLLEXPORT void StringToLong(const wxChar *s, long *number);
137 WXDLLEXPORT wxChar* IntToString(int number);
138 WXDLLEXPORT wxChar* LongToString(long number);
139
140 // Convert 2-digit hex number to decimal
141 WXDLLEXPORT int wxHexToDec(const wxString& buf);
142
143 // Convert decimal integer to 2-character hex string
144 WXDLLEXPORT void wxDecToHex(int dec, wxChar *buf);
145 WXDLLEXPORT wxString wxDecToHex(int dec);
146
147 // ----------------------------------------------------------------------------
148 // Process management
149 // ----------------------------------------------------------------------------
150
151 // Execute another program. Returns 0 if there was an error, a PID otherwise.
152 WXDLLEXPORT long wxExecute(wxChar **argv, bool sync = FALSE,
153 wxProcess *process = (wxProcess *) NULL);
154 WXDLLEXPORT long wxExecute(const wxString& command, bool sync = FALSE,
155 wxProcess *process = (wxProcess *) NULL);
156
157 // execute the command capturing its output into an array line by line
158 WXDLLEXPORT long wxExecute(const wxString& command,
159 wxArrayString& output);
160
161 // also capture stderr
162 WXDLLEXPORT long wxExecute(const wxString& command,
163 wxArrayString& output,
164 wxArrayString& error);
165
166 enum wxSignal
167 {
168 wxSIGNONE = 0, // verify if the process exists under Unix
169 wxSIGHUP,
170 wxSIGINT,
171 wxSIGQUIT,
172 wxSIGILL,
173 wxSIGTRAP,
174 wxSIGABRT,
175 wxSIGIOT = wxSIGABRT, // another name
176 wxSIGEMT,
177 wxSIGFPE,
178 wxSIGKILL,
179 wxSIGBUS,
180 wxSIGSEGV,
181 wxSIGSYS,
182 wxSIGPIPE,
183 wxSIGALRM,
184 wxSIGTERM
185
186 // further signals are different in meaning between different Unix systems
187 };
188
189 enum wxKillError
190 {
191 wxKILL_OK, // no error
192 wxKILL_BAD_SIGNAL, // no such signal
193 wxKILL_ACCESS_DENIED, // permission denied
194 wxKILL_NO_PROCESS, // no such process
195 wxKILL_ERROR // another, unspecified error
196 };
197
198 // send the given signal to the process (only NONE and KILL are supported under
199 // Windows, all others mean TERM), return 0 if ok and -1 on error
200 //
201 // return detailed error in rc if not NULL
202 WXDLLEXPORT int wxKill(long pid,
203 wxSignal sig = wxSIGTERM,
204 wxKillError *rc = NULL);
205
206 // Execute a command in an interactive shell window (always synchronously)
207 // If no command then just the shell
208 WXDLLEXPORT bool wxShell(const wxString& command = wxEmptyString);
209
210 // As wxShell(), but must give a (non interactive) command and its output will
211 // be returned in output array
212 WXDLLEXPORT bool wxShell(const wxString& command, wxArrayString& output);
213
214 // Sleep for nSecs seconds
215 WXDLLEXPORT void wxSleep(int nSecs);
216
217 // Sleep for a given amount of milliseconds
218 WXDLLEXPORT void wxUsleep(unsigned long milliseconds);
219
220 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
221 WXDLLEXPORT long wxGetFreeMemory();
222
223 // should wxApp::OnFatalException() be called?
224 WXDLLEXPORT bool wxHandleFatalExceptions(bool doit = TRUE);
225
226 // ----------------------------------------------------------------------------
227 // Environment variables
228 // ----------------------------------------------------------------------------
229
230 // returns TRUE if variable exists (value may be NULL if you just want to check
231 // for this)
232 WXDLLEXPORT bool wxGetEnv(const wxString& var, wxString *value);
233
234 // set the env var name to the given value, return TRUE on success
235 WXDLLEXPORT bool wxSetEnv(const wxString& var, const wxChar *value);
236
237 // remove the env var from environment
238 inline bool wxUnsetEnv(const wxString& var) { return wxSetEnv(var, NULL); }
239
240 // ----------------------------------------------------------------------------
241 // Network and username functions.
242 // ----------------------------------------------------------------------------
243
244 // NB: "char *" functions are deprecated, use wxString ones!
245
246 // Get eMail address
247 WXDLLEXPORT bool wxGetEmailAddress(wxChar *buf, int maxSize);
248 WXDLLEXPORT wxString wxGetEmailAddress();
249
250 // Get hostname.
251 WXDLLEXPORT bool wxGetHostName(wxChar *buf, int maxSize);
252 WXDLLEXPORT wxString wxGetHostName();
253
254 // Get FQDN
255 WXDLLEXPORT wxString wxGetFullHostName();
256 WXDLLEXPORT bool wxGetFullHostName(wxChar *buf, int maxSize);
257
258 // Get user ID e.g. jacs (this is known as login name under Unix)
259 WXDLLEXPORT bool wxGetUserId(wxChar *buf, int maxSize);
260 WXDLLEXPORT wxString wxGetUserId();
261
262 // Get user name e.g. Julian Smart
263 WXDLLEXPORT bool wxGetUserName(wxChar *buf, int maxSize);
264 WXDLLEXPORT wxString wxGetUserName();
265
266 // Get current Home dir and copy to dest (returns pstr->c_str())
267 WXDLLEXPORT wxString wxGetHomeDir();
268 WXDLLEXPORT const wxChar* wxGetHomeDir(wxString *pstr);
269
270 // Get the user's home dir (caller must copy --- volatile)
271 // returns NULL is no HOME dir is known
272 #if defined(__UNIX__) && wxUSE_UNICODE
273 WXDLLEXPORT const wxMB2WXbuf wxGetUserHome(const wxString& user = wxEmptyString);
274 #else
275 WXDLLEXPORT wxChar* wxGetUserHome(const wxString& user = wxEmptyString);
276 #endif
277
278 // get number of total/free bytes on the disk where path belongs
279 WXDLLEXPORT bool wxGetDiskSpace(const wxString& path,
280 wxLongLong *pTotal = NULL,
281 wxLongLong *pFree = NULL);
282
283 #if wxUSE_GUI // GUI only things from now on
284
285 // ----------------------------------------------------------------------------
286 // Menu accelerators related things
287 // ----------------------------------------------------------------------------
288
289 WXDLLEXPORT wxChar* wxStripMenuCodes(const wxChar *in, wxChar *out = (wxChar *) NULL);
290 WXDLLEXPORT wxString wxStripMenuCodes(const wxString& str);
291
292 #if wxUSE_ACCEL
293 class WXDLLEXPORT wxAcceleratorEntry;
294 WXDLLEXPORT wxAcceleratorEntry *wxGetAccelFromString(const wxString& label);
295 #endif // wxUSE_ACCEL
296
297 // ----------------------------------------------------------------------------
298 // Window search
299 // ----------------------------------------------------------------------------
300
301 // Find the window/widget with the given title or label.
302 // Pass a parent to begin the search from, or NULL to look through
303 // all windows.
304 WXDLLEXPORT wxWindow* wxFindWindowByLabel(const wxString& title, wxWindow *parent = (wxWindow *) NULL);
305
306 // Find window by name, and if that fails, by label.
307 WXDLLEXPORT wxWindow* wxFindWindowByName(const wxString& name, wxWindow *parent = (wxWindow *) NULL);
308
309 // Returns menu item id or -1 if none.
310 WXDLLEXPORT int wxFindMenuItemId(wxFrame *frame, const wxString& menuString, const wxString& itemString);
311
312 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
313 // is always present but may be less reliable than a native version.
314 WXDLLEXPORT wxWindow* wxGenericFindWindowAtPoint(const wxPoint& pt);
315 WXDLLEXPORT wxWindow* wxFindWindowAtPoint(const wxPoint& pt);
316
317 // ----------------------------------------------------------------------------
318 // Message/event queue helpers
319 // ----------------------------------------------------------------------------
320
321 // Yield to other apps/messages
322 WXDLLEXPORT bool wxYield();
323
324 // Like wxYield, but fails silently if the yield is recursive.
325 WXDLLEXPORT bool wxYieldIfNeeded();
326
327 // Yield to other apps/messages and disable user input
328 WXDLLEXPORT bool wxSafeYield(wxWindow *win = NULL);
329
330 // Enable or disable input to all top level windows
331 WXDLLEXPORT void wxEnableTopLevelWindows(bool enable = TRUE);
332
333 // Check whether this window wants to process messages, e.g. Stop button
334 // in long calculations.
335 WXDLLEXPORT bool wxCheckForInterrupt(wxWindow *wnd);
336
337 // Consume all events until no more left
338 WXDLLEXPORT void wxFlushEvents();
339
340 // a class which disables all windows (except, may be, thegiven one) in its
341 // ctor and enables them back in its dtor
342 class WXDLLEXPORT wxWindowDisabler
343 {
344 public:
345 wxWindowDisabler(wxWindow *winToSkip = (wxWindow *)NULL);
346 ~wxWindowDisabler();
347
348 private:
349 wxWindowList *m_winDisabled;
350 };
351
352 // ----------------------------------------------------------------------------
353 // Cursors
354 // ----------------------------------------------------------------------------
355
356 // Set the cursor to the busy cursor for all windows
357 class WXDLLEXPORT wxCursor;
358 WXDLLEXPORT_DATA(extern wxCursor*) wxHOURGLASS_CURSOR;
359 WXDLLEXPORT void wxBeginBusyCursor(wxCursor *cursor = wxHOURGLASS_CURSOR);
360
361 // Restore cursor to normal
362 WXDLLEXPORT void wxEndBusyCursor();
363
364 // TRUE if we're between the above two calls
365 WXDLLEXPORT bool wxIsBusy();
366
367 // Convenience class so we can just create a wxBusyCursor object on the stack
368 class WXDLLEXPORT wxBusyCursor
369 {
370 public:
371 wxBusyCursor(wxCursor* cursor = wxHOURGLASS_CURSOR)
372 { wxBeginBusyCursor(cursor); }
373 ~wxBusyCursor()
374 { wxEndBusyCursor(); }
375
376 // FIXME: These two methods are currently only implemented (and needed?)
377 // in wxGTK. BusyCursor handling should probably be moved to
378 // common code since the wxGTK and wxMSW implementations are very
379 // similar except for wxMSW using HCURSOR directly instead of
380 // wxCursor.. -- RL.
381 static const wxCursor &GetStoredCursor();
382 static const wxCursor GetBusyCursor();
383 };
384
385
386 // ----------------------------------------------------------------------------
387 // Reading and writing resources (eg WIN.INI, .Xdefaults)
388 // ----------------------------------------------------------------------------
389
390 #if wxUSE_RESOURCES
391 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file = wxEmptyString);
392 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file = wxEmptyString);
393 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file = wxEmptyString);
394 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file = wxEmptyString);
395
396 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file = wxEmptyString);
397 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file = wxEmptyString);
398 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file = wxEmptyString);
399 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file = wxEmptyString);
400 #endif // wxUSE_RESOURCES
401
402 void WXDLLEXPORT wxGetMousePosition( int* x, int* y );
403
404 // MSW only: get user-defined resource from the .res file.
405 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
406 #ifdef __WXMSW__
407 WXDLLEXPORT extern const wxChar* wxUserResourceStr;
408 WXDLLEXPORT wxChar* wxLoadUserResource(const wxString& resourceName, const wxString& resourceType = wxUserResourceStr);
409 #endif // MSW
410
411 // ----------------------------------------------------------------------------
412 // Display and colorss (X only)
413 // ----------------------------------------------------------------------------
414
415 #ifdef __WXGTK__
416 void *wxGetDisplay();
417 #endif
418
419 #ifdef __X__
420 WXDisplay *wxGetDisplay();
421 bool wxSetDisplay(const wxString& display_name);
422 wxString wxGetDisplayName();
423 #endif // X or GTK+
424
425 #ifdef __X__
426
427 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
428 // The resulting warnings are switched off here
429 #pragma message disable nosimpint
430 #endif
431 // #include <X11/Xlib.h>
432 #ifdef __VMS__
433 #pragma message enable nosimpint
434 #endif
435
436 #endif //__X__
437
438 #endif // wxUSE_GUI
439
440 // ----------------------------------------------------------------------------
441 // Error message functions used by wxWindows (deprecated, use wxLog)
442 // ----------------------------------------------------------------------------
443
444 // Format a message on the standard error (UNIX) or the debugging
445 // stream (Windows)
446 WXDLLEXPORT void wxDebugMsg(const wxChar *fmt ...);
447
448 // Non-fatal error (continues)
449 WXDLLEXPORT_DATA(extern const wxChar*) wxInternalErrorStr;
450 WXDLLEXPORT void wxError(const wxString& msg, const wxString& title = wxInternalErrorStr);
451
452 // Fatal error (exits)
453 WXDLLEXPORT_DATA(extern const wxChar*) wxFatalErrorStr;
454 WXDLLEXPORT void wxFatalError(const wxString& msg, const wxString& title = wxFatalErrorStr);
455
456
457 #endif
458 // _WX_UTILSH__