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