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