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