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