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