]> git.saurik.com Git - wxWidgets.git/blob - include/wx/utils.h
wxUSE_XML and wxUSE_XRC build fixes if expat is not enabled
[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 backwars 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 killing -pid kills
175 // all children as well as pid
176 wxEXEC_MAKE_GROUP_LEADER = 4
177 };
178
179 // Execute another program.
180 //
181 // If flags contain wxEXEC_SYNC, return -1 on failure and the exit code of the
182 // process if everything was ok. Otherwise (i.e. if wxEXEC_ASYNC), return 0 on
183 // failure and the PID of the launched process if ok.
184 WXDLLIMPEXP_BASE long wxExecute(wxChar **argv, int flags = wxEXEC_ASYNC,
185 wxProcess *process = (wxProcess *) NULL);
186 WXDLLIMPEXP_BASE long wxExecute(const wxString& command, int flags = wxEXEC_ASYNC,
187 wxProcess *process = (wxProcess *) NULL);
188
189 // execute the command capturing its output into an array line by line, this is
190 // always synchronous
191 WXDLLIMPEXP_BASE long wxExecute(const wxString& command,
192 wxArrayString& output);
193
194 // also capture stderr (also synchronous)
195 WXDLLIMPEXP_BASE long wxExecute(const wxString& command,
196 wxArrayString& output,
197 wxArrayString& error);
198
199 enum wxSignal
200 {
201 wxSIGNONE = 0, // verify if the process exists under Unix
202 wxSIGHUP,
203 wxSIGINT,
204 wxSIGQUIT,
205 wxSIGILL,
206 wxSIGTRAP,
207 wxSIGABRT,
208 wxSIGIOT = wxSIGABRT, // another name
209 wxSIGEMT,
210 wxSIGFPE,
211 wxSIGKILL,
212 wxSIGBUS,
213 wxSIGSEGV,
214 wxSIGSYS,
215 wxSIGPIPE,
216 wxSIGALRM,
217 wxSIGTERM
218
219 // further signals are different in meaning between different Unix systems
220 };
221
222 enum wxKillError
223 {
224 wxKILL_OK, // no error
225 wxKILL_BAD_SIGNAL, // no such signal
226 wxKILL_ACCESS_DENIED, // permission denied
227 wxKILL_NO_PROCESS, // no such process
228 wxKILL_ERROR // another, unspecified error
229 };
230
231 enum wxShutdownFlags
232 {
233 wxSHUTDOWN_POWEROFF, // power off the computer
234 wxSHUTDOWN_REBOOT // shutdown and reboot
235 };
236
237 // Shutdown or reboot the PC
238 WXDLLIMPEXP_BASE bool wxShutdown(wxShutdownFlags wFlags);
239
240 // send the given signal to the process (only NONE and KILL are supported under
241 // Windows, all others mean TERM), return 0 if ok and -1 on error
242 //
243 // return detailed error in rc if not NULL
244 WXDLLIMPEXP_BASE int wxKill(long pid,
245 wxSignal sig = wxSIGTERM,
246 wxKillError *rc = NULL);
247
248 // Execute a command in an interactive shell window (always synchronously)
249 // If no command then just the shell
250 WXDLLIMPEXP_BASE bool wxShell(const wxString& command = wxEmptyString);
251
252 // As wxShell(), but must give a (non interactive) command and its output will
253 // be returned in output array
254 WXDLLIMPEXP_BASE bool wxShell(const wxString& command, wxArrayString& output);
255
256 // Sleep for nSecs seconds
257 WXDLLIMPEXP_BASE void wxSleep(int nSecs);
258
259 // Sleep for a given amount of milliseconds
260 WXDLLIMPEXP_BASE void wxMilliSleep(unsigned long milliseconds);
261
262 // Sleep for a given amount of microseconds
263 WXDLLIMPEXP_BASE void wxMicroSleep(unsigned long microseconds);
264
265 // Sleep for a given amount of milliseconds (old, bad name), use wxMilliSleep
266 wxDEPRECATED( WXDLLIMPEXP_BASE void wxUsleep(unsigned long milliseconds) );
267
268 // Get the process id of the current process
269 WXDLLIMPEXP_BASE unsigned long wxGetProcessId();
270
271 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
272 WXDLLIMPEXP_BASE long wxGetFreeMemory();
273
274 // should wxApp::OnFatalException() be called?
275 WXDLLIMPEXP_BASE bool wxHandleFatalExceptions(bool doit = TRUE);
276
277 // ----------------------------------------------------------------------------
278 // Environment variables
279 // ----------------------------------------------------------------------------
280
281 // returns TRUE if variable exists (value may be NULL if you just want to check
282 // for this)
283 WXDLLIMPEXP_BASE bool wxGetEnv(const wxString& var, wxString *value);
284
285 // set the env var name to the given value, return TRUE on success
286 WXDLLIMPEXP_BASE bool wxSetEnv(const wxString& var, const wxChar *value);
287
288 // remove the env var from environment
289 inline bool wxUnsetEnv(const wxString& var) { return wxSetEnv(var, NULL); }
290
291 // ----------------------------------------------------------------------------
292 // Network and username functions.
293 // ----------------------------------------------------------------------------
294
295 // NB: "char *" functions are deprecated, use wxString ones!
296
297 // Get eMail address
298 WXDLLIMPEXP_BASE bool wxGetEmailAddress(wxChar *buf, int maxSize);
299 WXDLLIMPEXP_BASE wxString wxGetEmailAddress();
300
301 // Get hostname.
302 WXDLLIMPEXP_BASE bool wxGetHostName(wxChar *buf, int maxSize);
303 WXDLLIMPEXP_BASE wxString wxGetHostName();
304
305 // Get FQDN
306 WXDLLIMPEXP_BASE wxString wxGetFullHostName();
307 WXDLLIMPEXP_BASE bool wxGetFullHostName(wxChar *buf, int maxSize);
308
309 // Get user ID e.g. jacs (this is known as login name under Unix)
310 WXDLLIMPEXP_BASE bool wxGetUserId(wxChar *buf, int maxSize);
311 WXDLLIMPEXP_BASE wxString wxGetUserId();
312
313 // Get user name e.g. Julian Smart
314 WXDLLIMPEXP_BASE bool wxGetUserName(wxChar *buf, int maxSize);
315 WXDLLIMPEXP_BASE wxString wxGetUserName();
316
317 // Get current Home dir and copy to dest (returns pstr->c_str())
318 WXDLLIMPEXP_BASE wxString wxGetHomeDir();
319 WXDLLIMPEXP_BASE const wxChar* wxGetHomeDir(wxString *pstr);
320
321 // Get the user's home dir (caller must copy --- volatile)
322 // returns NULL is no HOME dir is known
323 #if defined(__UNIX__) && wxUSE_UNICODE
324 WXDLLIMPEXP_BASE const wxMB2WXbuf wxGetUserHome(const wxString& user = wxEmptyString);
325 #else
326 WXDLLIMPEXP_BASE wxChar* wxGetUserHome(const wxString& user = wxEmptyString);
327 #endif
328
329 // get number of total/free bytes on the disk where path belongs
330 WXDLLIMPEXP_BASE bool wxGetDiskSpace(const wxString& path,
331 wxLongLong *pTotal = NULL,
332 wxLongLong *pFree = NULL);
333
334 #if wxUSE_GUI // GUI only things from now on
335
336 // ----------------------------------------------------------------------------
337 // Menu accelerators related things
338 // ----------------------------------------------------------------------------
339
340 WXDLLEXPORT wxChar* wxStripMenuCodes(const wxChar *in, wxChar *out = (wxChar *) NULL);
341 WXDLLEXPORT wxString wxStripMenuCodes(const wxString& str);
342
343 #if wxUSE_ACCEL
344 class WXDLLEXPORT wxAcceleratorEntry;
345 WXDLLEXPORT wxAcceleratorEntry *wxGetAccelFromString(const wxString& label);
346 #endif // wxUSE_ACCEL
347
348 // ----------------------------------------------------------------------------
349 // Window search
350 // ----------------------------------------------------------------------------
351
352 // Returns menu item id or -1 if none.
353 WXDLLEXPORT int wxFindMenuItemId(wxFrame *frame, const wxString& menuString, const wxString& itemString);
354
355 // Find the wxWindow at the given point. wxGenericFindWindowAtPoint
356 // is always present but may be less reliable than a native version.
357 WXDLLEXPORT wxWindow* wxGenericFindWindowAtPoint(const wxPoint& pt);
358 WXDLLEXPORT wxWindow* wxFindWindowAtPoint(const wxPoint& pt);
359
360 // NB: this function is obsolete, use wxWindow::FindWindowByLabel() instead
361 //
362 // Find the window/widget with the given title or label.
363 // Pass a parent to begin the search from, or NULL to look through
364 // all windows.
365 WXDLLEXPORT wxWindow* wxFindWindowByLabel(const wxString& title, wxWindow *parent = (wxWindow *) NULL);
366
367 // NB: this function is obsolete, use wxWindow::FindWindowByName() instead
368 //
369 // Find window by name, and if that fails, by label.
370 WXDLLEXPORT wxWindow* wxFindWindowByName(const wxString& name, wxWindow *parent = (wxWindow *) NULL);
371
372 // ----------------------------------------------------------------------------
373 // Message/event queue helpers
374 // ----------------------------------------------------------------------------
375
376 // Yield to other apps/messages and disable user input
377 WXDLLEXPORT bool wxSafeYield(wxWindow *win = NULL, bool onlyIfNeeded = FALSE);
378
379 // Enable or disable input to all top level windows
380 WXDLLEXPORT void wxEnableTopLevelWindows(bool enable = TRUE);
381
382 // Check whether this window wants to process messages, e.g. Stop button
383 // in long calculations.
384 WXDLLEXPORT bool wxCheckForInterrupt(wxWindow *wnd);
385
386 // Consume all events until no more left
387 WXDLLEXPORT void wxFlushEvents();
388
389 // a class which disables all windows (except, may be, thegiven one) in its
390 // ctor and enables them back in its dtor
391 class WXDLLEXPORT wxWindowDisabler
392 {
393 public:
394 wxWindowDisabler(wxWindow *winToSkip = (wxWindow *)NULL);
395 ~wxWindowDisabler();
396
397 private:
398 wxWindowList *m_winDisabled;
399
400 DECLARE_NO_COPY_CLASS(wxWindowDisabler)
401 };
402
403 // ----------------------------------------------------------------------------
404 // Cursors
405 // ----------------------------------------------------------------------------
406
407 // Set the cursor to the busy cursor for all windows
408 class WXDLLEXPORT wxCursor;
409 WXDLLEXPORT_DATA(extern wxCursor*) wxHOURGLASS_CURSOR;
410 WXDLLEXPORT void wxBeginBusyCursor(wxCursor *cursor = wxHOURGLASS_CURSOR);
411
412 // Restore cursor to normal
413 WXDLLEXPORT void wxEndBusyCursor();
414
415 // TRUE if we're between the above two calls
416 WXDLLEXPORT bool wxIsBusy();
417
418 // Convenience class so we can just create a wxBusyCursor object on the stack
419 class WXDLLEXPORT wxBusyCursor
420 {
421 public:
422 wxBusyCursor(wxCursor* cursor = wxHOURGLASS_CURSOR)
423 { wxBeginBusyCursor(cursor); }
424 ~wxBusyCursor()
425 { wxEndBusyCursor(); }
426
427 // FIXME: These two methods are currently only implemented (and needed?)
428 // in wxGTK. BusyCursor handling should probably be moved to
429 // common code since the wxGTK and wxMSW implementations are very
430 // similar except for wxMSW using HCURSOR directly instead of
431 // wxCursor.. -- RL.
432 static const wxCursor &GetStoredCursor();
433 static const wxCursor GetBusyCursor();
434 };
435
436
437 // ----------------------------------------------------------------------------
438 // Reading and writing resources (eg WIN.INI, .Xdefaults)
439 // ----------------------------------------------------------------------------
440
441 #if wxUSE_RESOURCES
442 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file = wxEmptyString);
443 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file = wxEmptyString);
444 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file = wxEmptyString);
445 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file = wxEmptyString);
446
447 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file = wxEmptyString);
448 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file = wxEmptyString);
449 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file = wxEmptyString);
450 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file = wxEmptyString);
451 #endif // wxUSE_RESOURCES
452
453 void WXDLLEXPORT wxGetMousePosition( int* x, int* y );
454
455 // MSW only: get user-defined resource from the .res file.
456 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
457 #ifdef __WXMSW__
458 WXDLLEXPORT extern const wxChar* wxUserResourceStr;
459 WXDLLEXPORT wxChar* wxLoadUserResource(const wxString& resourceName, const wxString& resourceType = wxUserResourceStr);
460 #endif // MSW
461
462 // ----------------------------------------------------------------------------
463 // Display and colorss (X only)
464 // ----------------------------------------------------------------------------
465
466 #ifdef __WXGTK__
467 void *wxGetDisplay();
468 #endif
469
470 #ifdef __X__
471 WXDisplay *wxGetDisplay();
472 bool wxSetDisplay(const wxString& display_name);
473 wxString wxGetDisplayName();
474 #endif // X or GTK+
475
476 #ifdef __X__
477
478 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
479 // The resulting warnings are switched off here
480 #pragma message disable nosimpint
481 #endif
482 // #include <X11/Xlib.h>
483 #ifdef __VMS__
484 #pragma message enable nosimpint
485 #endif
486
487 #endif //__X__
488
489 #endif // wxUSE_GUI
490
491 // ----------------------------------------------------------------------------
492 // wxYield(): these functions are obsolete, please use wxApp methods instead!
493 // ----------------------------------------------------------------------------
494
495 // Yield to other apps/messages
496 WXDLLIMPEXP_BASE bool wxYield();
497
498 // Like wxYield, but fails silently if the yield is recursive.
499 WXDLLIMPEXP_BASE bool wxYieldIfNeeded();
500
501 // ----------------------------------------------------------------------------
502 // Error message functions used by wxWidgets (deprecated, use wxLog)
503 // ----------------------------------------------------------------------------
504
505 #if WXWIN_COMPATIBILITY_2_2
506
507 // Format a message on the standard error (UNIX) or the debugging
508 // stream (Windows)
509 WXDLLIMPEXP_BASE void wxDebugMsg(const wxChar *fmt ...) ATTRIBUTE_PRINTF_1;
510
511 // Non-fatal error (continues)
512 WXDLLIMPEXP_DATA_BASE(extern const wxChar*) wxInternalErrorStr;
513 WXDLLIMPEXP_BASE void wxError(const wxString& msg, const wxString& title = wxInternalErrorStr);
514
515 // Fatal error (exits)
516 WXDLLIMPEXP_DATA_BASE(extern const wxChar*) wxFatalErrorStr;
517 WXDLLIMPEXP_BASE void wxFatalError(const wxString& msg, const wxString& title = wxFatalErrorStr);
518
519 #endif // WXWIN_COMPATIBILITY_2_2
520
521 #endif
522 // _WX_UTILSH__