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