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