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