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