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