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