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