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