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