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