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