]> git.saurik.com Git - wxWidgets.git/blob - include/wx/utils.h
renamed WXDLLEXPORT_BASE/CORE to WXDLLIMPEXP_BASE/CORE
[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 WXDLLIMPEXP_BASE wxProcess;
43 class WXDLLIMPEXP_BASE wxFrame;
44 class WXDLLIMPEXP_BASE wxWindow;
45 class WXDLLIMPEXP_BASE wxWindowList;
46 class WXDLLIMPEXP_BASE 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 WXDLLIMPEXP_DATA_BASE(extern wxChar*) wxBuffer;
63 #endif
64
65 // Make a copy of this string using 'new'
66 WXDLLIMPEXP_BASE wxChar* copystring(const wxChar *s);
67
68 #if WXWIN_COMPATIBILITY_2
69 // Matches string one within string two regardless of case
70 WXDLLIMPEXP_BASE 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 WXDLLIMPEXP_BASE void wxBell();
82
83 // Get OS description as a user-readable string
84 WXDLLIMPEXP_BASE wxString wxGetOsDescription();
85
86 // Get OS version
87 WXDLLIMPEXP_BASE int wxGetOsVersion(int *majorVsn = (int *) NULL,
88 int *minorVsn = (int *) NULL);
89
90 // Return a string with the current date/time
91 WXDLLIMPEXP_BASE wxString wxNow();
92
93 // Return path where wxWindows is installed (mostly useful in Unices)
94 WXDLLIMPEXP_BASE const wxChar *wxGetInstallPrefix();
95 // Return path to wxWin data (/usr/share/wx/%{version}) (Unices)
96 WXDLLIMPEXP_BASE 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 WXDLLIMPEXP_DATA_BASE(extern const wxChar*) wxFloatToStringStr;
134 WXDLLIMPEXP_DATA_BASE(extern const wxChar*) wxDoubleToStringStr;
135
136 WXDLLIMPEXP_BASE void StringToFloat(const wxChar *s, float *number);
137 WXDLLIMPEXP_BASE wxChar* FloatToString(float number, const wxChar *fmt = wxFloatToStringStr);
138 WXDLLIMPEXP_BASE void StringToDouble(const wxChar *s, double *number);
139 WXDLLIMPEXP_BASE wxChar* DoubleToString(double number, const wxChar *fmt = wxDoubleToStringStr);
140 WXDLLIMPEXP_BASE void StringToInt(const wxChar *s, int *number);
141 WXDLLIMPEXP_BASE void StringToLong(const wxChar *s, long *number);
142 WXDLLIMPEXP_BASE wxChar* IntToString(int number);
143 WXDLLIMPEXP_BASE wxChar* LongToString(long number);
144
145 #endif // WXWIN_COMPATIBILITY_2_4
146
147 // Convert 2-digit hex number to decimal
148 WXDLLIMPEXP_BASE int wxHexToDec(const wxString& buf);
149
150 // Convert decimal integer to 2-character hex string
151 WXDLLIMPEXP_BASE void wxDecToHex(int dec, wxChar *buf);
152 WXDLLIMPEXP_BASE 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 WXDLLIMPEXP_BASE long wxExecute(wxChar **argv, int flags = wxEXEC_ASYNC,
184 wxProcess *process = (wxProcess *) NULL);
185 WXDLLIMPEXP_BASE 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 WXDLLIMPEXP_BASE long wxExecute(const wxString& command,
191 wxArrayString& output);
192
193 // also capture stderr (also synchronous)
194 WXDLLIMPEXP_BASE 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 WXDLLIMPEXP_BASE 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 WXDLLIMPEXP_BASE 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 WXDLLIMPEXP_BASE 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 WXDLLIMPEXP_BASE bool wxShell(const wxString& command, wxArrayString& output);
254
255 // Sleep for nSecs seconds
256 WXDLLIMPEXP_BASE void wxSleep(int nSecs);
257
258 // Sleep for a given amount of milliseconds
259 WXDLLIMPEXP_BASE void wxUsleep(unsigned long milliseconds);
260
261 // Get the process id of the current process
262 WXDLLIMPEXP_BASE unsigned long wxGetProcessId();
263
264 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
265 WXDLLIMPEXP_BASE long wxGetFreeMemory();
266
267 // should wxApp::OnFatalException() be called?
268 WXDLLIMPEXP_BASE 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 WXDLLIMPEXP_BASE bool wxGetEnv(const wxString& var, wxString *value);
277
278 // set the env var name to the given value, return TRUE on success
279 WXDLLIMPEXP_BASE 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 WXDLLIMPEXP_BASE bool wxGetEmailAddress(wxChar *buf, int maxSize);
292 WXDLLIMPEXP_BASE wxString wxGetEmailAddress();
293
294 // Get hostname.
295 WXDLLIMPEXP_BASE bool wxGetHostName(wxChar *buf, int maxSize);
296 WXDLLIMPEXP_BASE wxString wxGetHostName();
297
298 // Get FQDN
299 WXDLLIMPEXP_BASE wxString wxGetFullHostName();
300 WXDLLIMPEXP_BASE bool wxGetFullHostName(wxChar *buf, int maxSize);
301
302 // Get user ID e.g. jacs (this is known as login name under Unix)
303 WXDLLIMPEXP_BASE bool wxGetUserId(wxChar *buf, int maxSize);
304 WXDLLIMPEXP_BASE wxString wxGetUserId();
305
306 // Get user name e.g. Julian Smart
307 WXDLLIMPEXP_BASE bool wxGetUserName(wxChar *buf, int maxSize);
308 WXDLLIMPEXP_BASE wxString wxGetUserName();
309
310 // Get current Home dir and copy to dest (returns pstr->c_str())
311 WXDLLIMPEXP_BASE wxString wxGetHomeDir();
312 WXDLLIMPEXP_BASE 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 WXDLLIMPEXP_BASE const wxMB2WXbuf wxGetUserHome(const wxString& user = wxEmptyString);
318 #else
319 WXDLLIMPEXP_BASE wxChar* wxGetUserHome(const wxString& user = wxEmptyString);
320 #endif
321
322 // get number of total/free bytes on the disk where path belongs
323 WXDLLIMPEXP_BASE 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 // Yield to other apps/messages and disable user input
370 WXDLLEXPORT bool wxSafeYield(wxWindow *win = NULL, bool onlyIfNeeded = FALSE);
371
372 // Enable or disable input to all top level windows
373 WXDLLEXPORT void wxEnableTopLevelWindows(bool enable = TRUE);
374
375 // Check whether this window wants to process messages, e.g. Stop button
376 // in long calculations.
377 WXDLLEXPORT bool wxCheckForInterrupt(wxWindow *wnd);
378
379 // Consume all events until no more left
380 WXDLLEXPORT void wxFlushEvents();
381
382 // a class which disables all windows (except, may be, thegiven one) in its
383 // ctor and enables them back in its dtor
384 class WXDLLEXPORT wxWindowDisabler
385 {
386 public:
387 wxWindowDisabler(wxWindow *winToSkip = (wxWindow *)NULL);
388 ~wxWindowDisabler();
389
390 private:
391 wxWindowList *m_winDisabled;
392
393 DECLARE_NO_COPY_CLASS(wxWindowDisabler)
394 };
395
396 // ----------------------------------------------------------------------------
397 // Cursors
398 // ----------------------------------------------------------------------------
399
400 // Set the cursor to the busy cursor for all windows
401 class WXDLLEXPORT wxCursor;
402 WXDLLEXPORT_DATA(extern wxCursor*) wxHOURGLASS_CURSOR;
403 WXDLLEXPORT void wxBeginBusyCursor(wxCursor *cursor = wxHOURGLASS_CURSOR);
404
405 // Restore cursor to normal
406 WXDLLEXPORT void wxEndBusyCursor();
407
408 // TRUE if we're between the above two calls
409 WXDLLEXPORT bool wxIsBusy();
410
411 // Convenience class so we can just create a wxBusyCursor object on the stack
412 class WXDLLEXPORT wxBusyCursor
413 {
414 public:
415 wxBusyCursor(wxCursor* cursor = wxHOURGLASS_CURSOR)
416 { wxBeginBusyCursor(cursor); }
417 ~wxBusyCursor()
418 { wxEndBusyCursor(); }
419
420 // FIXME: These two methods are currently only implemented (and needed?)
421 // in wxGTK. BusyCursor handling should probably be moved to
422 // common code since the wxGTK and wxMSW implementations are very
423 // similar except for wxMSW using HCURSOR directly instead of
424 // wxCursor.. -- RL.
425 static const wxCursor &GetStoredCursor();
426 static const wxCursor GetBusyCursor();
427 };
428
429
430 // ----------------------------------------------------------------------------
431 // Reading and writing resources (eg WIN.INI, .Xdefaults)
432 // ----------------------------------------------------------------------------
433
434 #if wxUSE_RESOURCES
435 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file = wxEmptyString);
436 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file = wxEmptyString);
437 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file = wxEmptyString);
438 WXDLLEXPORT bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file = wxEmptyString);
439
440 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file = wxEmptyString);
441 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file = wxEmptyString);
442 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file = wxEmptyString);
443 WXDLLEXPORT bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file = wxEmptyString);
444 #endif // wxUSE_RESOURCES
445
446 void WXDLLEXPORT wxGetMousePosition( int* x, int* y );
447
448 // MSW only: get user-defined resource from the .res file.
449 // Returns NULL or newly-allocated memory, so use delete[] to clean up.
450 #ifdef __WXMSW__
451 WXDLLEXPORT extern const wxChar* wxUserResourceStr;
452 WXDLLEXPORT wxChar* wxLoadUserResource(const wxString& resourceName, const wxString& resourceType = wxUserResourceStr);
453 #endif // MSW
454
455 // ----------------------------------------------------------------------------
456 // Display and colorss (X only)
457 // ----------------------------------------------------------------------------
458
459 #ifdef __WXGTK__
460 void *wxGetDisplay();
461 #endif
462
463 #ifdef __X__
464 WXDisplay *wxGetDisplay();
465 bool wxSetDisplay(const wxString& display_name);
466 wxString wxGetDisplayName();
467 #endif // X or GTK+
468
469 #ifdef __X__
470
471 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
472 // The resulting warnings are switched off here
473 #pragma message disable nosimpint
474 #endif
475 // #include <X11/Xlib.h>
476 #ifdef __VMS__
477 #pragma message enable nosimpint
478 #endif
479
480 #endif //__X__
481
482 #endif // wxUSE_GUI
483
484 // ----------------------------------------------------------------------------
485 // wxYield(): these functions are obsolete, please use wxApp methods instead!
486 // ----------------------------------------------------------------------------
487
488 // Yield to other apps/messages
489 WXDLLIMPEXP_BASE bool wxYield();
490
491 // Like wxYield, but fails silently if the yield is recursive.
492 WXDLLIMPEXP_BASE bool wxYieldIfNeeded();
493
494 // ----------------------------------------------------------------------------
495 // Error message functions used by wxWindows (deprecated, use wxLog)
496 // ----------------------------------------------------------------------------
497
498 #if WXWIN_COMPATIBILITY_2_2
499
500 // Format a message on the standard error (UNIX) or the debugging
501 // stream (Windows)
502 WXDLLIMPEXP_BASE void wxDebugMsg(const wxChar *fmt ...) ATTRIBUTE_PRINTF_1;
503
504 // Non-fatal error (continues)
505 WXDLLIMPEXP_DATA_BASE(extern const wxChar*) wxInternalErrorStr;
506 WXDLLIMPEXP_BASE void wxError(const wxString& msg, const wxString& title = wxInternalErrorStr);
507
508 // Fatal error (exits)
509 WXDLLIMPEXP_DATA_BASE(extern const wxChar*) wxFatalErrorStr;
510 WXDLLIMPEXP_BASE void wxFatalError(const wxString& msg, const wxString& title = wxFatalErrorStr);
511
512 #endif // WXWIN_COMPATIBILITY_2_2
513
514 #endif
515 // _WX_UTILSH__