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